【发布时间】:2016-12-07 02:29:18
【问题描述】:
我有一个带有复选框列的GridView。单击按钮时,应删除选中复选框的所有行。我不知何故偶然发现了一个奇怪而骇人的解决方案,我不知道它为什么会起作用。我已经搜索过相关的 SO 问题。
相关代码:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
' I have no idea why this is needed for the checkboxes to work...
Dim x = imageGridView.Rows
End Sub
Protected Sub RemoveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles removeButton.Click
For Each row As GridViewRow In imageGridView.Rows
Dim selectCheckBox As CheckBox = DirectCast(row.Cells(0).FindControl("selectCheckBox"), CheckBox)
If selectCheckBox.Checked Then
Dim fileName As String = row.Cells(1).Text
ImageList.Remove(ImageList.FindLast(Function(r) r.FileName = fileName))
End If
Next
imageGridView.DataSource = ImageList
imageGridView.DataBind()
End Sub
aspx:
<asp:GridView ID="imageGridView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="selectCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
要删除的行需要Dim x = imageGridView.Rows 行。我在Page_Init sub 中尝试了我的RemoveButton_Click 代码后发现了这一点,然后删除代码直到它不再工作。 Dim x = imageGridView 是不够的,在Page_Load 做同样的事情是不行的。
我的复选框永远不会被禁用。
那么,简单地说,为什么我必须在 Page_Init 中引用 imageGridView.Rows 才能使我的代码正常工作?
【问题讨论】:
-
您是在进行完整的回发,还是使用更新面板?
-
我有一个
UpdatePaneladdButton触发,但网格不包含在面板中。
标签: asp.net vb.net checkbox postback