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