无法在GridView中检测CheckBox检查方法

时间:2013-11-22 19:26:04

标签: vb.net gridview checkbox

我在gridview中添加了复选框。我想从每一行获取第三个单元格值。我正在使用按钮单击以回发到gridview之外的服务器。 我的代码:

(ASPX部分)

<asp:TemplateField HeaderStyle-Width="8px">
 <ItemTemplate >

                      <asp:CheckBox runat="server" ID="chkRow"/>
 </ItemTemplate>
 </asp:TemplateField>
'It is outside of the Gridview
<asp:Button ID="btnGetSelected" runat="server" Text="Get selected records"  OnClick ="GetSelectedRecords" />

(Vb.net Part)

Protected Sub GetSelectedRecords(sender As Object, e As EventArgs)
    Dim dt As New DataTable()
    Dim colSelRowKeys As New Collection
    colSelRowKeys.Clear()
    Dim s As String
    For Each row As GridViewRow In GridADFMain.Rows
 If (CType(row.FindControl("chkRow"), CheckBox)).Checked Then
            s = row.Cells(3).Text
            colSelRowKeys.Add(s)
 End If
    Next
    Session.Add("colSelRowKeys", colSelRowKeys)
End Sub

我无法输入if条件。

1 个答案:

答案 0 :(得分:0)

您发布的代码在测试中效果很好。确保您没有在回发后重新绑定Gridview。在重新绑定复选框中,将覆盖值。检查你对gridview进行数据绑定的位置,通常在Page_Load中,将代码包装在If Not IsPostBack中,如下所示:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        'Code to get data
        '    ..........            
        ' BindGridView()
        GridADFMain.DataBind()
    End If
End Sub

希望它有所帮助!