2014-03-13 35 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 

這必將解決您的問題