asp.net gridview Select all, deselect, delete selected items __.net

Source: Internet
Author: User

asp.net gridview control Select all, uncheck, delete the implementation of the selected item

1. Add a template column to the GridView control, add a CheckBox control to the column, and add the desired button, as shown in the figure:


2. Select All Events

<span style= "FONT-SIZE:18PX;" >//all
        protected void Btn_all_click (object sender, EventArgs e)
        {for
            (int i = 0; I <= GridView2.Rows.Cou nt-1; i++)
            {
                checkbox cbox = (checkbox) Gridview2.rows[i]. FindControl ("CheckBox1");
                Cbox. Checked = true;
            }
        } </span>
3, Cancel the event

<span style= "FONT-SIZE:18PX;" >//Cancel
        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. Anti-election events

<span style= "FONT-SIZE:18PX;" >//Anti-select
        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. Delete selected items (principle: traverse all the checkbox in the GridView control)

<span style= "FONT-SIZE:18PX;" >//Delete selected
        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)
                {
                   //here performs the delete operation
                }
            }
           
        }</span>





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.