How to get CheckBoxes values ​​inside gridview that are checked with asp.net

I use checkbox in gridview .... I use it in the first cell .... When I select the checkbox at runtime, I need to get these values ​​... but when I select or when I click on the checkbox, it does not find or the value takes like FALSE ... how to write in asp.net backend and in C # code?

<asp:TemplateField> <ItemTemplate > <asp:checkbox id="ShowAddress" runat="server" /> </ItemTemplate> </asp:TemplateField> 

Code for:

  protected void Button1_Click(object sender, EventArgs e) { // Looping through all the rows in the GridView foreach (GridViewRow di in GridView1.Rows) { CheckBox chkBx = (CheckBox)di.FindControl("ShowAddress"); if (chkBx != null && chkBx.Checked) { /// put your code here } } } 

Is there any implementation in the script when loading the page?

Can anyone help?

+6
c #
source share
5 answers

How do you populate your gridview? If you do this in Page_Load, make sure you do not do this in postback (check IsPostBack).

Is your chkBx variable null?

The following code works:

  protected void Button1_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { CheckBox chk = row.Cells[0].Controls[0] as CheckBox; if (chk != null && chk.Checked) { // ... } } } 
+4
source share
 StringCollection idCollection = new StringCollection(); string strID = string.Empty; for (int i = 0; i < GridView1.Rows.Count; i++) { CheckBox chkDelete = (CheckBox) GridView1.Rows.Cells[0].FindControl("chkSelect"); if (chkDelete != null) { if (chkDelete.Checked) { strID = GridView1.Rows.Cells[1].Text; idCollection.Add(strID); } } } 

For more information, see the link: http://www.itworld2.com/ghowto.aspx?id=69

+4
source share
 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Loadgridview();// its a correct }// not Loadgridview() here if you load above error is occur } 

check him

+1
source share
 int i = 0; foreach (GridViewRow row in GridView1.Rows) { CheckBox chk = (CheckBox)GridView_AdminTags.Rows[i].Cells[0].FindControl("chkTag"); if (chk != null) if (chk.Checked) { ////.......; } i++; } i = 0; 
0
source share

Jakob Answer will work if the bottom line is used. Even one control only in a cell, the index must be 1 not 0

 CheckBox chk = row.Cells[0].Controls[1] as CheckBox; 

Thanks. Sam

-one
source share

All Articles