如何获取使用asp .net检查的gridview中的CheckBoxes值

我在gridview中使用了复选框….我在第一个单元格中使用它….当我在运行时选择复选框时,我需要获取这些值…但是在选择或单击复选框时,它不是发现或价值是假的…如何用asp.net后端和c#代码写?

     

代码隐藏:

  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 } } } 

页面加载时脚本中是否有任何实现?

有人可以帮忙吗?

你如何填充你的GridView? 如果你在Page_Load中执行此操作,请确保您没有在回发中执行此操作(请检查IsPostBack)。

你的chkBx变量是null吗?

以下代码有效:

  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) { // ... } } } 
 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); } } } 

有关详细信息,请访问此链接: http : //www.itworld2.com/ghowto.aspx?id = 69

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

核实

 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; 

如果使用以下行,Jakob Answer将起作用。 即使一个控件只在单元格中,索引也需要为1而不是0

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

谢谢山姆