How to select all records during batch deletion [summary]

Source: Internet
Author: User

When you delete a large volume of data, if you have a select all button to select and delete all the records displayed on multiple pages, it is much more user-friendly than the deletion of individual items. Next, let's talk about how to achieve this, in fact, there are many searches on the Internet.ArticleAll of them are about batch deletion. They can be divided into two main categories: using JavaScript scripts to achieve full selection. 2: select all on the server
First, let's talk about how to use JavaScript to achieve full selection.
Drag a gridview to the page, set the data source, add a template column for The gridview, and add a chekcbox to the template column, such as the followingCode

< ASP: gridview ID = "Gridview1" Runat = "Server" Autogeneratecolumns = "False" Datakeynames = "Productid"
Performanceid = "Sqlperformance1" >
< Columns >
< ASP: boundfield Datafield = "Productid" Headertext = "Productid" Insertvisible = "False"
Readonly = "True" Sortexpression = "Productid"   />
< ASP: boundfield Datafield = "Productname" Headertext = "Productname" Sortexpression = "Productname"   />
< ASP: boundfield Datafield = "Supplierid" Headertext = "Supplierid" Sortexpression = "Supplierid"   />
< ASP: boundfield Datafield = "Categoryid" Headertext = "Categoryid" Sortexpression = "Categoryid"   />
< ASP: templatefield >
< Itemtemplate >
< ASP: checkbox ID = "Chk_del" Runat = "Server"   />
</ Itemtemplate >
</ ASP: templatefield >
</ Columns >
</ ASP: gridview >

Next, add a checkbox control to the page.
<Asp: checkbox id = "chk_js" runat = "server" text = "select all (JS)" onclick = "selectall (this)"/>
Write the following JS script for the control:

< Script Type = " Text/JavaScript " >
// Sets the selected status of all checkpoints Based on the selected status of the input checkbox.
Function Selectall (OBJ)
{
VaR Allinput = Document. getelementsbytagname ( " Input " );
// Alert (allinput. Length );
VaR Looptime = Allinput. length;
For (I =   0 ; I < Looptime; I ++ )
{
// Alert (allinput [I]. type );
If (Allinput [I]. Type =   " Checkbox " )
{
Allinput [I]. Checked=OBJ. checked;
}
}
}
</ Script >

The basic effect is available. Next we will try to embellish it. Add a button to the webpage
<Asp: button id = "btn_deleterecords" runat = "server" onclientclick = "Return judgeselect ();" text = "delete selected records"/>
Add JS script // Check whether the record is selected, and confirm the deletion.
Function Judgeselect ()
{
VaR Result =   False ;
VaR Allinput = Document. getelementsbytagname ( " Input " );
VaR Looptime = Allinput. length;
For (I =   0 ; I < Looptime; I ++ )
{
If (Allinput [I]. Checked)
{
Result= True;
Break;
}
}
If ( ! Result)
{
Alert ("Select the record to be deleted first!");
ReturnResult;
}
Result = Confirm ( " Are you sure you want to delete the selected record? " );
Return Result;
}

2. Select all through the server code
Add a checkbox to the page, set the autopostback attribute to true, and add the checkedchanged event to it.
<Asp: checkbox id = "chk_server" runat = "server" oncheckedchanged = "chk_server_checkedchanged" text = "select all (server)" autopostback = "true"/>
The Event code is as follows:

Int A =   This . Gridview1.rows. count;
For ( Int I =   0 ; I < A; I ++ )
{
Checkbox CHK=(Checkbox)This. Gridview1.rows [I]. findcontrol ("Chk_del");
Chk. Checked= This. Chk_server.checked;
}

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.