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)

点击事件

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
4

1 回答 1

0

每次创建页面时都需要重新创建动态控件,以允许 ASP.NET 将视图状态、回发数据和事件连接恢复到动态创建的控件。所以记得在回发时重新创建它们。

请检查您是否在每个回帖中创建它们。

于 2012-05-24T16:11:06.163 回答