Select All or cancel all entries in the gridview to delete multiple entries at the same time.

Source: Internet
Author: User

In our project background operations, multiple operations are inevitably deleted at the same time. In this article, when you click Select All, all items on the current page are selected. When you cancel the selection of a certain item, the checked value of the "select all" checkbox is also false. Then, the ID of the selected item is obtained in the background to delete multiple items at the same time.

 

Front-end code

Code highlighting produced by actipro codehighlighter (freeware) http://www.CodeHighlighter.com/--> 1 <% @ page Language = "C #" autoeventwireup = "true" codefile = "newsform. aspx. CS "inherits =" admin_newsform "%>

<! 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" Language = "JavaScript">
Function isornotcheckall (checkvalue)
{
VaR arrallcheckboxes = Document. getelementsbytagname ("input ");
// Check all
If (checkvalue = true)
{
For (I = 0; I <arrallcheckboxes. length; I ++)
{
If (document. getelementbyid (arrallcheckboxes [I]. ID). type = "checkbox ")
{
Document. getelementbyid (arrallcheckboxes [I]. ID). Checked = true;
}
}
}
// Do not check any one
If (checkvalue = false)
{
For (I = 0; I <arrallcheckboxes. length; I ++)
{
If (document. getelementbyid (arrallcheckboxes [I]. ID). type = "checkbox ")
{
Document. getelementbyid (arrallcheckboxes [I]. ID). Checked = false;
}
}
}
}
Function cbdeleteclick (clientid, checkallclientid)
{
VaR arrallcheckboxes = Document. getelementsbytagname ("input ");
// There has some one not be checked
If (document. getelementbyid (clientid). Checked = false)
{
For (I = 0; I <arrallcheckboxes. length; I ++)
{
If (arrallcheckboxes [I]. Id. indexof ("checkall ")! =-1)
{
Arrallcheckboxes [I]. Checked = false;
}
}
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: Image id = "image1" runat = "server" imageurl = "hximages/button/btn_add.gif"/>
<Asp: imagebutton id = "btndelete" runat = "server"
Imageurl = "~ /Admin/hximages/button/btn_del.gif "onclick =" btndelete_click "/>
<Asp: gridview id = "gvnews" runat = "server" autogeneratecolumns = "false" datakeynames = "newsid"
Width = "100%" onrowdatabound = "gvnews_rowdatabound"
Onrowdeleting = "gvnews_rowdeleting" allowpaging = "true"
Onpageindexchanging = "gvnews_pageindexchanging" pagesize = "3">
<Columns>
<Asp: templatefield>
<Headertemplate>
Select All
<Asp: checkbox id = "checkall" runat = "server" onclick = "javascript: Return isornotcheckall (this. Checked);"/>
</Headertemplate>
<Itemtemplate>
<Asp: checkbox id = "cbdelete" runat = "server"/>
</Itemtemplate>
<Itemstyle horizontalalign = "center"/>
</ASP: templatefield>

<Asp: boundfield datafield = "newsid" headertext = "no.">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: boundfield datafield = "newstitle" headertext = "title">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: templatefield headertext = "add date">
<Itemstyle horizontalalign = "center"/>
<Itemtemplate>
<% # Convert. todatetime (eval ("newsdate"). tostring ("yyyy-mm-dd") %>
</Itemtemplate>
</ASP: templatefield>
<Asp: boundfield datafield = "newsdegree" headertext = "Number of clicks (times)">
<Itemstyle horizontalalign = "center"/>
</ASP: boundfield>
<Asp: templatefield headertext = "operation">
<Itemstyle horizontalalign = "center"/>
<Itemtemplate>
<Asp: imagebutton id = "imgdel" Title = "delete" commandname = "delete" imageurl = "hximages/del.gif" runat = "server"/>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
</Form>
</Body>
</Html>

 

Background code

Code highlighting produced by actipro codehighlighter (freeware) http://www.CodeHighlighter.com/--> 1 using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
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. xml. LINQ;

Public partial class admin_newsform: system. Web. UI. Page
{
String SQL = NULL;
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Bindgvnews ();
Btndelete. Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete it? ');");
}
}
Void bindgvnews ()
{
SQL = string. Format ("select * From newstable order by newsid DESC ");
Datatable dt = database. executedataset (SQL). Tables [0];
Gvnews. datasource = DT. defaultview;
Gvnews. databind ();
}
Protected void gvnews_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
String newsid = gvnews. datakeys [E. rowindex]. value. tostring ();
SQL = string. Format ("delete from newstable where newsid = {0}", convert. toint32 (newsid ));
Int I = database. executenonquery (SQL );
If (I> 0)
{
Bindgvnews ();
}
}
Protected void gvnews_pageindexchanging (Object sender, gridviewpageeventargs E)
{
Gvnews. pageindex = E. newpageindex;
Bindgvnews ();
}
Protected void gvnews_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
(Imagebutton) E. Row. findcontrol ("imgdel"). Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete it? ')");
(Checkbox) E. row. findcontrol ("cbdelete ")). attributes. add ("onclick", "cbdeleteclick ('" + (checkbox) E. row. findcontrol ("cbdelete ")). clientid + "')");
}
}
String getdeleteids ()
{
String IDs = NULL;
Foreach (gridviewrow gvr in gvnews. Rows)
{
Checkbox cb = (checkbox) gvr. findcontrol ("cbdelete ");
If (CB. Checked)
{
If (IDs = NULL)
{
IDS = gvnews. datakeys [gvr. rowindex]. value. tostring ();
}
Else
{
IDS = IDs + "," + gvnews. datakeys [gvr. rowindex]. value. tostring ();
}
}
}
Return IDs;
}
Protected void btndelete_click (Object sender, imageclickeventargs E)
{
String newsids = This. getdeleteids ();
If (newsids! = NULL)
{
SQL = string. Format ("delete from newstable where newsid in ({0})", newsids );
Int I = database. executenonquery (SQL );
If (I> 0)
{
Bindgvnews ();
}
}
Else
{
Response. Write ("<SCRIPT> alert ('specify the item to delete! '); </SCRIPT> ");
}
}
}

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.