![](/img/trans.png)
[英]How to add event for Checkbox click in Asp.net Gridview Column
[英]How to add checkbox in Asp.net Gridview in all header columns
我是asp.net开发人员的新手。
借助数据表填充了一个asp.net网格视图,我的所有列都是动态的。 现在,我想在所有列中添加一个复选框,动态bot不想添加行。 一次,我只能选中一个复选框。在这种情况下,如果我选择第二次复选框,则应取消选中第一次选中的复选框。
我的代码如下:
protected void dgvWoList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i] != null && (!e.Row.Cells[i].IsNullOrEmpty()))
{
CheckBox chk = new CheckBox();
chk.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Controls.Add(chk);
}
}
}
}
现在,我试图取消选中该复选框,但无法执行。 请帮我。
答案:非常感谢您的答复。 Spl感谢AnthonyBCodes的建议,而不是使用复选框单选按钮,它解决了我的问题。 我将代码从复选框更改为单选按钮,如下所示。
RadioButton chk = new RadioButton();
chk.GroupName = "radio";
chk.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Controls.Add(chk);
(已在评论中回答,并由OP编辑成问题。移至社区Wiki答案。请参阅无答案的问题,但问题已在评论中解决(或扩展为聊天) )
OP写道:
非常感谢您的回复。 特别感谢@AnthonyBCodes的建议,而不是使用复选框来解决我的问题,而不是使用复选框。 我将代码从复选框更改为单选按钮,如下所示。
RadioButton chk = new RadioButton();
chk.GroupName = "radio";
chk.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Controls.Add(chk);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.