2014-03-13 39 views
0

我有一个GridViewCheckbox 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> 

回答

0

提供的信息,这听起来好像你是重新绑定在回发GridView控件。

检查你的数据绑定代码中的回传。

If Not IsPostBack() Then Me.GridView1.DataBind() End If

+0

我应该在哪里找到先生?在Page_Load上? – user3093453

+0

什么是gridview的数据源?你是否在后面的代码中设置它?如果是这样,你应该将调用放在Me.Gridview1.DataBind()在上面的例子中的这个方法。 – angusf

0

如果你填写在页面加载电网然后使用

If Not IsPostBack() Then 
GridView1.DataBind() <if your you are populating data through class just add it> 
or 
populate(param1, param2,...) 
End If 

这必将解决您的问题