2012-12-29 68 views
0

檢索我有一個網格,檢查的ID不能從一個GridView

<Columns> 
      <asp:TemplateField HeaderStyle-Width="20px"> 
       <ItemTemplate> 
        <asp:CheckBox ID="ChkSelect" runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Id" Visible="false"> 
       <ItemTemplate> 
        <asp:Label ID="LbLId" runat="server" Text='<%# Bind("ID") %>'></asp:Label> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:Label ID="LblId" runat="server" Text='<%# Bind("ID") %>'></asp:Label> 
       </EditItemTemplate> 
       <ItemStyle HorizontalAlign="Center" Width="20%" /> 
      </asp:TemplateField> 

我想要檢索的檢查複選框的ID。我曾嘗試實施這一代碼,

foreach (GridViewRow row in GvDDlToken.Rows) 
      { 
       CheckBox chk = row.Cells[0].Controls[0] as CheckBox; 
       if (chk != null && chk.Checked) 
       { 

         string id = "," + row.Cells[1].Text; 


       } 
      } 

Checkbox chk值當屬無效,沒有對象,參考了。我在做什麼可能的錯誤?感謝您的幫助。

回答

1

你應該使用FindControl

樣本:

foreach (GridViewRow row in GvDDlToken.Rows) 
{ 
    if(((CheckBox)row.FindControl("CheckBox1")).Checked == true) 
    { 
    //some code 
    } 
}