我正在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部分)
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检查条件。
您发布的代码在测试中运行良好。确保你没有在后台重新绑定你的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
希望它能有所帮助!