日期:2014-05-20  浏览次数:20896 次

如何给Gridview 每个单元格添加一格checkbox

Gridview 每个单元格添加一格checkbox, 后在点一个Save按钮后怎么样来获取哪些checkbox是选中的

添加checkbox:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  for (int i = 0; i < e.Row.Cells.Count; i++)
  {
  e.Row.Cells[i].Wrap = false;
  }
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  //e.Row.Cells[5].BackColor = System.Drawing.Color.FromName("#FFBD9D");
  if (e.Row.RowIndex >= 0)
  {
  for (int j = 1; j < GridView1.Columns.Count; j++)
  {
  CheckBox cb = new CheckBox();
  cb.ID = "chk" + e.Row.RowIndex + j;
  e.Row.Cells[j].Controls.Add(cb);
   
  if (!cb.Checked)
  //e.Row.Cells[j].Text = "<input type='checkbox' name='chk' onclick='docheck(this)'>";
  e.Row.Cells[j].BackColor = System.Drawing.Color.FromName("#BBFFBB");  
  }  
  }
  }
  }


提取:
 protected void btnSave_Click(object sender, EventArgs e)
  {
  string strSql;
  for (int i = 1; i < GridView1.Columns.Count; i++)
  {
  string strPjcd = GridView1.Columns[i].HeaderText;
  for (int j = 0; j < GridView1.Rows.Count; j++)
  {
  string strPsnm = GridView1.Rows[i].Cells[0].Text;
  CheckBox ck = (CheckBox)GridView1.FindControl("chk" + j + i);
  if (ck.Checked)
  {
  strSql = "";
  }
  }
  }

  getData();
  }
为什么提取时ck 为null;求解决

------解决方案--------------------
用WPF吧,利用资源模板很容易实现的
------解决方案--------------------
使用
"<input type='checkbox' name='chk' onclick='docheck(this)'>";

Request.Form["chk"]

得到
------解决方案--------------------
使用
CheckBox cb = new CheckBox();
cb.ID = "chk" + e.Row.RowIndex + j;

的话

后台
for(int i = 0;i<Request.Form.Count;i++)
{
if(Request.Form.Keys[i].StartsWith("chk"))
Response.Write("<li>" + Request.Form.Keys[i].ToString() + " = " + Request.Form[i].ToString());
}