Batch deletion after the GridView is selected in ASP.net

Source: Internet
Author: User
<form id= "Form1" runat= "Server" >
<asp:button id= "Delall" runat= "server" text= "Delete selected" onclick= "Delall_click"
<div>
<asp:gridview id= "Gvuser" runat= "Server"
autogeneratecolumns= "False" datakeynames= "UserId"
Datasourceid= "Objectdatasourceuser" onrowdeleted= "gvuser_rowdeleted"
Cellpadding= "4" forecolor= "#333333" gridlines= "None" allowpaging= "True"
Pagesize= "3" >
<alternatingrowstyle backcolor= "White" forecolor= "#284775"/>

<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id= "Checkall" type= checkbox "onclick=" SelectAll (this); "/> Select All
</HeaderTemplate>
<ItemTemplate>
<asp:checkbox runat= "Server" id= "Checksingle"/>
</ItemTemplate>
<itemstyle width= "100px"/>

</asp:TemplateField><asp:boundfield datafield= "UserId" headertext= "UserId"
sortexpression= "UserId"/>
<asp:boundfield datafield= "UserName" headertext= "UserName"
sortexpression= "UserName"/>
<asp:boundfield datafield= "Userpass" headertext= "Userpass"
sortexpression= "Userpass"/>
<asp:boundfield datafield= "role" headertext= "role" sortexpression= "role"/>
<asp:boundfield datafield= "Regtime" headertext= "Regtime"
sortexpression= "Regtime"/>
<asp:boundfield datafield= "Lognum" headertext= "Lognum"
sortexpression= "Lognum"/>
<%--<asp:commandfield showeditbutton= "True"/>--%>
<asp:TemplateField>
<EditItemTemplate>
<asp:linkbutton id= "LinkButton4" runat= "Server" commandname= "Update" > Update </asp:LinkButton>
<asp:linkbutton id= "LinkButton5" runat= "Server" commandname= "Cancel" > Cancel </asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:linkbutton id= "LinkButton3" runat= "Server" commandname= "edit" > Edit </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:templatefield headertext= "Action" showheader= "False" >
<ItemTemplate>
<asp:linkbutton id= "Lbdelete" runat= "Server" causesvalidation= "False"
onclientclick= "return confirm (' Are you sure you want to delete it? ')" text= "Delete" commandname= "delete" ></asp:LinkButton>
</ItemTemplate>
<itemstyle horizontalalign= "Center" verticalalign= "Middle"/>
</asp:TemplateField>
</Columns>
<editrowstyle backcolor= "#999999"/>
<footerstyle backcolor= "#5D7B9D" font-bold= "True" forecolor= "white"/>
<pagerstyle backcolor= "#284775" forecolor= "white" horizontalalign= "Center"/>
<rowstyle backcolor= "#F7F6F3" forecolor= "#333333"/>
<selectedrowstyle backcolor= "#E2DED6" font-bold= "True" forecolor= "#333333"/>
<sortedascendingcellstyle backcolor= "#E9E7E2"/>
<sortedascendingheaderstyle backcolor= "#506C8C"/>
<sorteddescendingcellstyle backcolor= "#FFFDF8"/>
<sorteddescendingheaderstyle backcolor= "#6F8DAE"/>
</asp:GridView>
<asp:objectdatasource id= "Objectdatasourceuser" runat= "Server"
Dataobjecttypename= "User" deletemethod= "DeleteUser" insertmethod= "AddUser"
Selectmethod= "GetAllUsers" typename= "UserService" updatemethod= "ModifyUser" >
</asp:ObjectDataSource>
</div>
</form>
<script type= "Text/javascript" >
function SelectAll (obj) {
var chkall = obj;
state = chkall.checked;
Elem = chkall.form.elements;
for (i = 0; i < elem.length; i++) {
if (Elem[i].type = = "checkbox" && Elem[i]!= chkall.id) {
if (elem[i].checked!= state) {
Elem[i].click ();
}
}
}
}

</script>


protected void Delall_click (object sender, EventArgs e)
{
Int intcount = This.gvUser.Rows.Count;
for (int i = 0; i < intcount; i++)
{
CheckBox checksingle = this.gvuser.rows[i]. Cells[0]. FindControl ("Checksingle") as CheckBox;
if (checksingle.checked)
{
Userservice.deleteuserbyuserid (Convert.ToInt16 (this.gvuser.datakeys[i). Values[0]));
}
}
Post-delete Updates
Gvuser.databind ();
}

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.