GridView控件中添加一列CheckBox,如何描述checkbox被选中

2025-02-26 00:41:37
推荐回答(1个)
回答1:

///


/// 遍历GridView的方法
///

///
private ArrayList GetSelected()
{
ArrayList selectedItems = new ArrayList();
foreach (GridViewRow row in GridView1.Rows)
{
if (((CheckBox)row.FindControl("CheckBoxes")).Checked)
{
selectedItems.Add(row.Cells[0].Text.ToString());
}
}
return selectedItems;
}

然后当你选中其中一个或多个CheckBox以后,例如执行删除操作时,点击删除按钮触发事件的代码为:

protected void Button1_Click(object sender, EventArgs e)
{
try
{
ArrayList selectedItems = GetSelected();
if害川愤沸莅度缝砂俯棘 (selectedItems.Count == 0)
{
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('请选择至少一行数据')", true);
}
else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBoxes");

if (cb.Checked == true)
{
//你所执行的代码
}
}
BindData();
}
}
catch (Exception ex)
{
throw ex;
}
}

就是这样。