我有一个GridView
与Checkbox control
,我想获得选中的复选框,但是当我尝试使用For Each Loop
迭代Gridview
时,所检索的数据被复制。我真的不知道如何解决它。我有谷歌同样的问题或方法,这和我有相同的代码,所以我在这里失踪?这里是我的代码:对于Asp.net Gridview中的每个循环无法正常工作
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim chkRow As CheckBox = TryCast(row.Cells(0).FindControl("chkRow"), CheckBox)
If chkRow.Checked Then
Dim name As String = row.Cells(1).Text
Response.Write(name)
End If
End If
Next
这里是我的aspx
代码:
<asp:GridView ID="GridView1" CssClass="table table-bordered" runat="server"
CellPadding="4" ForeColor="#333333"
GridLines="None" Width="100%" DataKeyNames="ID"
AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Laboratory Name" HeaderText="Laboratory Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Price" HeaderText="Price" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
我应该在哪里找到先生?在Page_Load上? – user3093453
什么是gridview的数据源?你是否在后面的代码中设置它?如果是这样,你应该将调用放在Me.Gridview1.DataBind()在上面的例子中的这个方法。 – angusf