Select All in the GridView to complete batch approval or batch Deletion

Source: Internet
Author: User

In ASP. NET, we often use the GridView. When performing some operations, such as deleting the selected items, we will add check boxes. If we want to delete them in batches, what should I do? First, we need to add a hidden field on the page to store the ID corresponding to each row of data: [csharp] <asp: hiddenField ID = "hdfWPBH" runat = "server"/> The following is the GridView used to display data: [csharp] <asp: gridView ID = "GridView2" SkinID = "GridView1" DataKeyNames = "ID" runat = "server" onrowdatabound = "inline"> <RowStyle BackColor = "# f2f2f2"/> <AlternatingRowStyle BackColor = "# DCEEFA"/> <Columns> <asp: templateField HeaderText = "no."> <HeaderTemplate> <input type = "checkbox" Name = "BoxIdAll" id = "BoxIdAll" onclick = "onclicksel (); "/> </HeaderTemplate> <ItemTemplate> <input id =" BoxId "name =" BoxId "value = '<% # (Convert. toString (Eval ("ID "))) %> 'Type = "checkbox"/> </ItemTemplate> <ItemStyle Height = "25px" HorizontalAlign = "Center"/> <HeaderStyle Width = "3%" Height = "28px" backColor = "# 80B4CF" HorizontalAlign = "Center"/> </asp: templateField> <asp: TemplateField HeaderText = "no."> <It EmTemplate> <font color = "#000000" style = "font-size: 10px"> <% # GetCountCK () %> </font> </ItemTemplate> <ItemStyle Height = "30px" HorizontalAlign = "Center"/> <HeaderStyle Width = "5%" Height = "30px" BackColor = "# 80B4CF "HorizontalAlign =" Center "/> </asp: templateField> <asp: TemplateField HeaderText = "item name"> <ItemTemplate> <% # strTrim (Convert. toString (Eval ("WPMC") %> </ItemTemplate> <ItemStyle HorizontalAlig N = "Center"/> <HeaderStyle Width = "13%" HorizontalAlign = "center" BackColor = "# 80B4CF"/> </asp: templateField> </Columns> </asp: GridView> there is also a button for completing the review: [csharp] <asp: imageButton ID = "imgAuditPass" runat = "server" BorderColor = "# FFCCCC" BorderStyle = "Solid" BorderWidth = "1px" ImageUrl = "~ /CWQLC/images/sure.png "onclick =" imgAuditPass_Click "ToolTip =" Click here to complete information Review "OnClientClick = 'Return batchAudit (this. id) '/> www.2cto.com: you also need to add some Javascript code: the check box event that implements the full selection function in the Header area: [javascript] function onclicksel () {var chkobj = document. getElementById ("BoxIdAll"); if (chkobj. checked = true) {selAll () ;}else {removeAll () ;}the following functions are called: [javascript] function selAll () {var selobj = document. getEl EmentsByName ("BoxId"); for (var I = 0; I <selobj. length; I ++) {if (! Selobj [I]. disabled) {selobj [I]. checked = true ;}} function removeAll () {var selobj = document. getElementsByName ("BoxId"); for (var I = 0; I <selobj. length; I ++) {selobj [I]. checked = false;} client events (OnClientClick = 'Return batchAudit (this. id) ') implements client verification. The Javascript code is [javascript] function batchAudit (id) {var AuditVal = ""; var bid = document. getElementsByName ("BoxId"); for (var I = 0; I <Bid. length; I ++) {if (bid [I]. checked = true) {AuditVal = AuditVal + bid [I]. value + "," ;}} if (AuditVal. length <= 0) {alert ("select the item to be reviewed first"); return false;} else {if (id = "btnDelete ") {if (confirm ("are you sure you want to review? ") {Document. getElementById ("hdfWPBH "). value = AuditVal; return true;} return false ;}} is in the background. cs "approved" button click event (onclick = "imgAuditPass_Click") C # code: [csharp] protected void imgAuditPass_Click (object sender, ImageClickEventArgs e) {string [] chkIds = null; string batchRegroup = Request. form ["hdfWPBH"]. toString (). trim (). trimEnd (','); // you can obtain the content of the hidden domain in the front-end if (batchRegroup. length! = 0) {chkIds = batchRegroup. split (',');} string SQL = ""; for (int I = 0; I <chkIds. length; I ++) {SQL = "UPDATE T_WPXX_CK set spr = '" + userrealName + "'where ID ='" + chkIds [I] + "'"; wpck. auditOrDelete (SQL); // input the SQL statement and execute} ClientScript. registerStartupScript (this. getType (), "pass", "alert ('approved! '); ", True); GridViewShow_CK (); // The display method of the Data bound to the GridView}. To do this, click the" Review passed "button, you can put the ID of the row selected by the check box in the hidden domain, as long as confirm ("are you sure you want to review it? ") Select" yes "to call the onclike event in the background and execute

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.