2012-05-24 36 views
0

我有一個動態表正在page_load上構建。動態圖像按鈕沒有觸發的事件處理程序

我把表放在更新面板中,這樣我就可以動態添加行而不需要整頁回發。每行有4個單元格,其中3個包含文本框,第4個單元格是需要刪除該行的圖像按鈕。

但是,刪除行的圖像按鈕回發但未觸發事件處理程序子。

我將表格設置爲會話變量以在回發上重新加載。

<%--Update Panel with Image Button to add row--%> 
<asp:UpdatePanel ID="pnlHA" runat="server" UpdateMode="conditional" > 
<ContentTemplate> 
    <asp:ImageButton ID="imgAddHA" AlternateText="Add Row" ImageUrl="../images/plus_orange.png" style="width:17px; height:17px; cursor: hand;" runat="server" /> 
    <div style="height:100px; overflow:scroll; overflow-x:hidden;"> 
     <span runat="server" id="spanTBL_HA" /> 
    </div> 
</ContentTemplate> 
</asp:UpdatePanel> 

上page.init如果PNL頁面在回發然後重新創建跨度

If pnlHA.Page.IsPostBack Then 
     spanTBL_HA.Controls.Clear() 
     spanTBL_HA.Controls.Add(CType(Session("tblHA"), Table)) 
    End If 

圖像按鈕被添加到表格單元格

img = New ImageButton 

Session("tblHA_Counter") = CInt(Session("tblHA_Counter")) + 1 

img.AlternateText = "Delete Row" 
img.Attributes.Add("style", "height: 10px; width: 10px; cursor: hand;") 
img.ImageUrl = "../images/minus_red.png" 
img.ID = "img_" & CInt(Session("tblHA_Counter")).ToString 
AddHandler img.Click, AddressOf imgRemove_Click 
img.Attributes.Add("runat", "server") 

tc.Attributes.Add("style", "width:35px") 
tc.Controls.Add(img) 

Click事件

Protected Sub imgRemove_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs) 
    Dim img As ImageButton = CType(sender, ImageButton) 
    Dim delRow As TableRow = Nothing 
    Dim rowIndex As Integer = 0 

    For Each row As TableRow In CType(Session("tblHA"), Table).Rows 
     If CType(row.Cells(3).Controls(0), ImageButton).UniqueID = img.UniqueID Then 
      delRow = row 
     End If 
    Next 

    If delRow IsNot Nothing Then 
     CType(Session("tblHA"), Table).Rows.Remove(delRow) 
    End If 

    spanTBL_HA.Controls.Clear() 
    spanTBL_HA.Controls.Add(CType(Session("tblHA"), Table)) 

End Sub 

回答

0

需要動態控件每創建一次創建頁面時允許ASP.NET將視圖狀態,回發數據和事件連接恢復到動態創建的控件。所以請記住在回發中重新創建它們。

請檢查您是否在每一個回帖中創建它們。

+0

初始表在page_load上創建並設置爲會話變量,然後添加到範圍。 span_tblHA.Controls.Add(CType(Session(「tblHA」),Table)))在page_init回發期間,我將span控制重置爲會話。這不符合「重新創建」的要求嗎? – Dan

相關問題