asp.net Girdview Learning six use template columns to implement multiple-row deletion

Source: Internet
Author: User
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using system.web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Collections.Generic;
12
13public Partial class GridViewTemplateTest:System.Web.UI.Page
14 {
15//Current page number, starting from 0
private int Curpage
17 {
Get
19 {
return viewstate["curpage"] = = null? 0:convert.toint32 (viewstate["Curpage"]);
21}
Set
23 {
viewstate["curpage"] = value;
25}
26}
protected void Page_Load (object sender, EventArgs e)
28 {
if (! IsPostBack)
30 {
Clientinfoaccessobj accessor = new Clientinfoaccessobj ();
curpage = 0; Show first page Gridview1.datasource = accessor. Getallclients ();
Gridview1.databind ();
35}
36}
Notoginseng protected void Btndeleteselectedinfo_click (object sender, EventArgs e)
38 {
list<int> selectedclientids = Getselectedclientids ();
if (selectedclientids.count = 0)
The return;
42
Clientinfoaccessobj accessor = new Clientinfoaccessobj ();
foreach (int id in selectedclientids)
45 {
Accessor. Deleteclientinfoforid (ID);
47}
Clientscript.registerstartupscript (this. GetType (), "info", "alert (' record deleted successfully ');", true);
49
50//calculation should show which page
' If ' (selectedclientids.count = = gridview1.pagesize)//Select All records on full page
if (curpage!= 0)
curpage--; Show Previous Page
54
55
56//Rebind data and display
Gridview1.datasource = accessor. Getallclients ();
Gridview1.pageindex = Curpage;
Gridview1.databind ();
60}
61
Getselectedclientids Private list<int> ()
63 {
list<int> ids = new list<int> ();
65//Loop to find the selected row
GridViewRow foreach (GVR in gridview1.rows) 67 {
68//Is the data row
The IF (GVR). RowType = = Datacontrolrowtype.datarow)
70 {
71//Find the specified control based on the control ID in the template column
A CheckBox chk = Gvr. FindControl ("CheckBox1") as CheckBox;
The IF (chk!= null) && chk. Checked)
74//Remove the primary key from the selected row and add it to the collection
The IDs. ADD ((int) gridview1.datakeys[gvr. RowIndex]. Value);
76}
77}
The return IDs;
79}
protected void Gridview1_pageindexchanging (object sender, Gridviewpageeventargs e)
81 {
Clientinfoaccessobj accessor = new Clientinfoaccessobj ();
Gridview1.datasource = accessor. Getallclients ();
Gridview1.pageindex = E.newpageindex;
Gridview1.databind ();
86//Save current page number
Curpage = E.newpageindex;
88}
89}
90

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.