ASP.NET GridView全选,反选,删除选中项

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tingzhiyi/article/details/52059591

ASP.NET GridView控件全选,反选,删除选中项的实现

1、在GridVIew控件中加上一个模板列,并在该列中加入CheckBox控件,并加入需要的按钮,如图:


2、全选事件

<span style="font-size:18px;">//全选
        protected void btn_all_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
            {
                CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
                cbox.Checked = true;
            }
        }</span>
3、取消事件

<span style="font-size:18px;"> //取消
        protected void btn_cancel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
            {
                CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
                cbox.Checked = false;
            }
        }</span>
4、反选事件

<span style="font-size:18px;"> //反选
        protected void btn_others_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
            {
                CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
                if (cbox.Checked == true)
                {
                    cbox.Checked = false;
                }
                else
                {
                    cbox.Checked = true;
                }
            }
        }</span>
5、删除选中项(原理:遍历GridView控件中所有CheckBox)

<span style="font-size:18px;"> //删除选中项
        protected void btn_del_Click(object sender, EventArgs e)
        {
           
            for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
            {
                CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
                if (cbox.Checked==true)
                {
                   //这里执行删除操作
                }
            }
           
        }</span>





阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页