我有一個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