Batch Delete by Repeater

Source: Internet
Author: User
Tags javascript array

Code for implementing batch deletion of Asp.net repeater:

Batch Delete involved content:
1. patchwork of deleted SQL statements, such as delete from sosuo8 where ID in (, 5)
2. After you click batch Delete, You need to verify the selected items on the client and prompt that the delete operation cannot be recovered. If it is implemented on the server, it will be very unfriendly, and slow speed is also a problem. The effect is as follows:

Detailed code:
The body area of the ASPX page:

<Body> <Form ID = "form1" runat = "server"> <div> </div> <asp: repeater id = "rptnews" runat = "server" onprerender = "rptnews_prerender"> 

 

JS Code in the head area:

After you press "batch Delete", you must determine whether the checkbox in repeater is selected.

Onclientclick = "Return delit ()"

Javas code:

 

Function delit () {var cbxbool = 0; For (VAR I = 0; I <cbxarray. length; I ++) {var OBJ = document. getelementbyid (cbxarray [I]); If (obj. checked = true) {cbxbool = 1; break;} If (cbxbool = 1) {var result = confirm ("the operation cannot be restored. Are you sure you want to perform the current operation? "); If (result) {return true;} else {alert (" You have not selected "); Return false;} return false ;}

 

 

All. CS code:

 

Using system; using system. configuration; using system. data; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. data. sqlclient; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) {BIND () ;}// bind data private void BIND () {sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["conn"]. tostring (); Conn. open (); dataset DS = new dataset (); sqldataadapter da = new sqldataadapter ("select top 10 id_news _, title_news _ from news_sosuo8 _", Conn); DA. fill (DS); this. rptnews. datasource = Ds; // bind dataset This. rptnews. databind (); // start binding Conn. close ();} Protected void btndel_click (Object sender, eventargs e) {string delid = ""; // retrieve the selected item for (INT I = 0; I <this. rptnews. items. count; I ++) {checkbox cbx = (checkbox) rptnews. items [I]. findcontrol ("cbx"); label LBL = (Label) rptnews. items [I]. findcontrol ("LBL"); If (cbx! = NULL) {If (cbx. checked) {delid + = LBL. text + "," ;}}// remove the last one, delid = (delid + ")"). replace (",)", ""); response. write ("the delete statement is: delete news_sosuo8 _ Where id_news _ in (" + delid + ")"); // write the delete Statement by yourself. BIND ();} protected void rptnews_prerender (Object sender, eventargs e) {prerepater (rptnews, this);} // This is a common method used to call public static void prerepater (repeater, system. web. UI. page) {clientscriptmanager cs = page. clientscript; For (INT I = 0; I <repeater. items. count; I ++) {checkbox cbx = (checkbox) repeater. items [I]. findcontrol ("cbx"); // register the clientid of the corresponding server control to the client JavaScript Array CS. registerarraydeclaration ("cbxarray", String. concat ("'", cbx. clientid ,"'"));}}}

 

 

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.