ASP. NET adds all-selected batch Delete functions to DataGrid and repeater)

Source: Internet
Author: User

In many cases, we need the "select all" function when managing or viewing the list, which is very easy to implement in ASP. NET. Below I will demonstrate a little smallCodeThis function is implemented.
A small function of JS is used to achieve full selection:

Function checkall (E, itemname ){
VaR AA = Document. getelementsbyname (itemname );
If (AA = undefined) return;
For (VAR I = 0; I <AA. length; I ++) AA [I]. Checked = E. checked;
}


First look at the implementation process in ASP: repeater.

Add a fully selected checkbox in headertemplate:
<Input name = "chkall" value = "1" type = "checkbox" onclick = "checkall (this, 'item')"/>

When binding in itemtemplate, you only need to set the ID number of the current row for the input:
<TD align = "center"> <input name = "item" type = "checkbox" value = '<% # eval ("deal_id ") %> '/> </TD>

 

Then we create a linkbutton for "delete all" and execute a background code when you click it:
<Asp: linkbutton id = "lb_bak_delete" text = "batch Delete" oncommand = "linkbutton_command" runat = "server" commandname = "delete" visible = "false"/>

The name of the executed function is linkbutton_command. The main purpose of setting the commandname parameter for linkbutton_command is to mark this operation as a delete function (usually generate, highlight, and other operations ), check the execution of linkbutton_command:
Protected void linkbutton_command (Object sender, commandeventargs e ){
String IDs = request. Form ["item"];
Switch (E. commandname ){
Case "delete ":
Bakdelete (IDS );
Break;
}
Data_list_load (); // reload
Return;

}
Private void bakdelete (string IDs ){
// Odeal is a class
If (! String. isnullorempty (IDS) & odeal. Delete ("[deal_id] In (" + IDs + ")")){
Outmess ("batch data deletion succeeded ");
} Else {
Outmess ("batch deletion failed ");
}
}


Then let's look at the method in the DataGrid or dataview:

The JS is still the same, as shown on the front-end:
<Asp: templatecolumn headertext = "<input name = 'chkall' value = '1' type = 'checkbox' onclick = 'checkall (this, js_item_text) '/> "itemstyle-width =" 30px "itemstyle-horizontalalign =" center "headerstyle-horizontalalign =" center "headerstyle-cssclass =" head ">
<Itemtemplate> <input name = "item" type = "checkbox" value = '<% # eval ("td_id") %>'/> </itemtemplate>
</ASP: templatecolumn>

Note that there is a single quotation mark in the checkall execution method, which cannot be added here. Declare a JS variable first, and then write a JS client to register when loading the list:
When listing classgriddatabind, add:

String sscript = "Var js_item_text = 'item ';";
Clientscriptmanager objcsm = page. clientscript;
If (! Objcsm. isstartupscriptregistered ("_ js_item_text ")){
Objcsm. registerstartupscript (this. GetType (), "_ js_item_text", sscript, true );
}

 

And the rest are the same as those of repeater.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/xielingxu/archive/2008/07/28/2726089.aspx

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.