C# gridview标题模板中的foreach复选框
我有一个动态gridview,标题有多个复选框。是否有一种方法可以让复选框单独执行以下操作:C# gridview标题模板中的foreach复选框,c#,asp.net,gridview,checkbox,C#,Asp.net,Gridview,Checkbox,我有一个动态gridview,标题有多个复选框。是否有一种方法可以让复选框单独执行以下操作: protected void gvUsers_OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { foreach (CheckBox cb in e.Row.Controls) //
protected void gvUsers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (CheckBox cb in e.Row.Controls) //<- is there something like this?
{
cb.Attributes.Add("onclick", "javascript:SelectAll(this);");
}
}
}
因为ID是动态生成的 您可以使用:
e.Row.FindControl("checkboxID");
var allCheckBoxes = e.Row.Cells.Cast<DataControlFieldCell>()
.SelectMany(c => c.Controls.OfType<CheckBox>());
foreach (CheckBox cb in allCheckBoxes)
{
cb.Attributes.Add("onclick", "javascript:SelectAll(this);");
}
var allCheckBoxes = from cell in e.Row.Cells.Cast<DataControlFieldCell>()
from cb in cell.Controls.OfType<CheckBox>()
select cb;