JS calls C # background code---JS implementation of the DataGrid "Select All", "reverse", call back code bulk delete data

Source: Internet
Author: User
Tags call back eval touch trim

Used to be web, basically no what JS, this short time, the company inside contact with JS quite a lot, they call me in the DataGrid inside the checkbox to get a "select All", to use JS to achieve, to achieve bulk deletion, this function, directly in C # is very good to achieve, Direct traversal can take the selected ID all out. JS, that can only use the HTML checkbox, this is not a server control, C # background code is inaccessible, so, and then found some information on the Internet, realized.

First, define a DataGrid control, first add a template column, which contains a checkbox for HTML, Attributes name=choessall, (NAME,JS code to use after) Id=chkchoessall, The unique identification of the bound data, here is the ID.

The HTML (. aspx) code is as follows:







< Asp:gridview ID = " Gvuint " runat = " Server " AutoGenerateColumns = " False " Width = " 100% " >
< Columns >
< Asp:templatefield HeaderText = " Select " >
< ItemTemplate >
< Input ID = " Chkchoessall " name = " Choessall " value = '' <%# DataBinder.Eval (Container.DataItem, "ID")%> '' type = " checkbox "   />
</ ItemTemplate >
</ Asp:templatefield >
< Asp:boundfield DataField = " ID " HeaderText = " Property name "   />
< Asp:boundfield DataField = " Address " HeaderText = " Address "   />
</ Columns >
</ Asp:gridview >

Add a HiddenField control (HTML control) property id=Hfid, (used after ID,JS code), and the ID number of the data in the checked check.

Now implement the JS code:







< Script Type = " Text/javascript " >
// Select All
         function Checkall_click ()
... {
if(document.form1.ChoessAll.length)
...{
document.getElementById ("Hfid"). Value= ",";
for (varI=0; I<Document.form1.choessall.length;i++)
... {
                     document.form1.choessall[i].checked  =   true ;
//                      document.getelementbyid ("Hfid"). Value = document.getelementbyid ("HfId"). Value  + document.form1.choessall[i].value;
//                      document.getelementbyid ("Hfid"). Value = document.getelementbyid ( "Hfid") .value +  ",";
                 }
}
Else
...{
document.form1.ChoessAll.checked = true;
}
//document.getElementById ("Btnshowid"). Click ();
        }
// anti-election
         function Checkrechange_click ()
... {
if(Document.form1.ChoessAll.length)
...{
document.getElementById ("Hfid"). Value= ",";
for (varI=0; I<Document.form1.choessall.length;i++)
...{
if(document.form1.choessall[i].checked)
...{
document.form1.choessall[i].checked = false;
}
Else
...{
document.form1.choessall[i].checked = true;
}
//document.getElementById ("Hfid"). Value = document.getElementById ("Hfid"). Value + Document.form1.Cho Essall[i].value;
//document.getElementById ("Hfid"). Value = document.getElementById ("Hfid"). Value + ",";
                }
}
Else
...{
document.form1.ChoessAll.checked = true;
}
//document.getElementById ("Btnshowid"). Click ();
        }
// Delete Data events (deleted according to the selection box)
         function Delete_click ()
... {
if(Document.form1.ChoessAll.length)
...{
varNum=0;
document.getElementById ("Hfid"). Value= ",";
for (varI=0; I<Document.form1.choessall.length;i++)
...{
if(document.form1.choessall[i].checked)
...{
document.getElementById ("Hfid"). Value=document.getElementById ("Hfid"). Value+Document.form1.choessall[i].value;
document.getElementById ("Hfid"). Value=document.getElementById ("Hfid"). Value+ ",";
Num=Num+ 1;
}
}
}
if(0==num)
...{
return alert (" no data is selected!") '' );
}
Else
... {
                  if (Confirm ( " ") Are you sure you want to delete the selected data? ' )
                 ... {
                      document.getElementById ( Btnshowid ). Click ();
                
            }
}
</ Script >

Here, just grab a control to touch the method "Select All" and "select" JS function







  < a href = " # " onclick = " Checkall_click (); " > Select All </ a >
< a href = " # " onclick = " Checkrechange_click (); " > anti-election </ a >

Here, you can already achieve the "Select All" and "reverse" function (see, is not no refresh).

Finally, realize, batch delete, here, use JS Touch background event, add a server control button,id=btnshowid(JS code to use), set him to hide, add an HTML code, to stimulate the click of this button event.







< a href = " # " onclick = " Delete_click (); " > Remove </ a >

To implement a background event for a button:







protected   void Btnshowid_click ( Object sender, EventArgs e)
... {
//The change here to the database operation can be implemented to delete the bulk
Labid.text=hfId.Value.ToString (). Trim ();
string[] Strparam=hfId.Value.ToString (). Split ('','');
for (intI= 0; I<strparam.length;++i)
...{
this. Response.Write (Strparam[i]. ToString (). Trim () + "<br>");
}}

This is the end of the function, and here's the full implementation code:

aspx







<% @ Page Language = " C # " AutoEventWireup = " true " CodeFile = " Default.aspx.cs " Inherits = " _default "   %>

<! DOCTYPE HTML Public " -//W3C//DTD XHTML 1.0 transitional//en "   " HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< Head runat = " Server " >
< title > No title page </ title >
< Script Type = " Text/javascript " >
// Select All
function Checkall_click ()
... {
if(Document.form1.ChoessAll.length)
...{
document.getElementById ("Hfid"). Value= ",";
for(var i=0; I<Document.form1.choessall.length;i++)
... {
                     document.form1.choessall[i]. checked   =   true ;
//                      document.getelementbyid ("Hfid") .value =  document.getElementById ("Hfid") .value + document.form1.choessall[i].value;
//                      document.getelementbyid ("Hfid") .value =  document.getElementById ("Hfid") .value +  ",";
                 }
}
Else
...{
Document.form1.ChoessAll. checked = true;
}
//document.getElementById ("Btnshowid"). Click ();
        }
// anti-election
function Checkrechange_click ()
... {
if(Document.form1.ChoessAll.length)
...{
document.getElementById ("Hfid"). Value= ",";
for(var i=0; I<Document.form1.choessall.length;i++)
...{
if(Document.form1.choessall[i].checked)
...{
Document.form1.choessall[i]. checked = false;
}
Else
...{
Document.form1.choessall[i]. checked = true;
}
//document.getElementById ("Hfid"). Value = document.getElementById ("Hfid"). Value + Document.form1.choessall[i].value;
//document.getElementById ("Hfid"). Value = document.getElementById ("Hfid"). Value + ",";
                }
}
Else
...{
Document.form1.ChoessAll. checked = true;
}
//document.getElementById ("Btnshowid"). Click ();
        }
// Delete Data events (deleted according to the selection box)
function Delete_click ()
... {
if(Document.form1.ChoessAll.length)
...{
var num=0;
document.getElementById ("Hfid"). Value= ",";
for(var i=0; I<Document.form1.choessall.length;i++)
...{
if(Document.form1.choessall[i].checked)
...{
document.getElementById ("Hfid"). Value=document.getElementById ("Hfid"). Value+Document.form1.choessall[i].value;
document.getElementById ("Hfid"). Value=document.getElementById ("Hfid"). Value+ ",";
Num=Num+ 1;
}
}
}
if(0==num)
...{
return alert (" no data is selected!") '' );
}
Else
...{
if(Confirm (' ) are you sure you want to delete the selected data ? '' ))
... {
document.getElementById ("btnshowid"). Click ();
}
}
}
</ Script >
</ Head >
< Body >
< Form ID = " Form1 " runat = " Server " >
< Div >
< a href = " # " onclick = " Delete_click (); " > Remove </ a >
</ Div >
< Div >
< Asp:gridview ID = " Gvuint " runat = " Server " AutoGenerateColumns = " False " Width = " 100% " >
< Columns >
< Asp:templatefield HeaderText = " Select " >
< ItemTemplate >
< Input ID = " Chkchoessall " name = " Choessall " value = '' <%# DataBinder.Eval (Container.DataItem, "ID")%> '' type = " checkbox "   />
</ ItemTemplate >
</ Asp:templatefield >
< Asp:boundfield DataField = " ID " HeaderText = " Property name "   />
< Asp:boundfield DataField = " Address " HeaderText = " Address "   />
</ Columns >
</ Asp:gridview >
</ Div >
< a href = " # " onclick = " Checkall_click (); " > Select All </ a >
< a href = " # " onclick = " Checkrechange_click (); " > anti-election </ a >
< Asp:label ID = " Labid " runat = " Server " ></ Asp:label >
< Div >
< Asp:hiddenfield ID = " Hfid " runat = " Server "   />
< Asp:button ID = " Btnshowid " runat = " Server " Text = " Btnshowid " OnClick = " Btnshowid_click " style = " Display:none " /></ Div >
</ form >
</ Body >
</ HTML >

Cs:







using System;
using System.Data;
using System.Configuration;
using system.web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

Public   Partial   class _default:system.web.ui.page
... {
protected voidPage_Load (Objectsender, EventArgs e)
...{

}
protected voidBtnshowid_click (Objectsender, EventArgs e)
...{
//The change here to the database operation can be implemented to delete the bulk
Labid.text=HfId.Value.ToString (). Trim ();
string[] Strparam=hfId.Value.ToString (). Split ('','');
for (intI= 0; I<strparam.length;++i)
...{
this. Response.Write (Strparam[i]. ToString (). Trim () + "<br>");
}
}
}
<
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.