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 //当前页码,从0开始
16 private int curPage
17 {
18 get
19 {
20 return ViewState["curPage"] == null ? 0 : Convert.ToInt32(ViewState["curPage"]);
21 }
22 set
23 {
24 ViewState["curPage"] = value;
25 }
26 }
27 protected void Page_Load(object sender, EventArgs e)
28 {
29 if (!IsPostBack)
30 {
31 ClientInfoAccessObj accessor = new ClientInfoAccessObj();
32 curPage = 0; //显示第一页
33 GridView1.DataSource = accessor.GetAllClients();
34 GridView1.DataBind();
35 }
36 }
37 protected void btnDeleteSelectedInfo_Click(object sender, EventArgs e)
38 {
39 List<int> SelectedClientIDs = GetSelectedClientIDs();
40 if (SelectedClientIDs.Count == 0)
41 return;
42
43 ClientInfoAccessObj accessor = new ClientInfoAccessObj();
44 foreach (int id in SelectedClientIDs)
45 {
46 accessor.DeleteClientInfoForID(id);
47 }
48 ClientScript.RegisterStartupScript(this.GetType(), "info", "alert('记录已成功删除');", true);
49
50 //计算应该显示哪一页
51 if (SelectedClientIDs.Count == GridView1.PageSize) //选中全页上的所有记录
52 if (curPage != 0)
53 curPage--; //显示前一页
54
55
56 //重新绑定数据并显示
57 GridView1.DataSource = accessor.GetAllClients();
58 GridView1.PageIndex = curPage;
59 GridView1.DataBind();
60 }
61
62 private List<int> GetSelectedClientIDs()
63 {
64 List<int> ids = new List<int>();
65 //循环查找被选中的行
66 foreach (GridViewRow gvr in GridView1.Rows)
67 {
68 //是数据行
69 if (gvr.RowType == DataControlRowType.DataRow)
70 {
71 //根据模板列中的控件ID查找指定的控件
72 CheckBox chk = gvr.FindControl("CheckBox1") as CheckBox;
73 if ((chk != null) && chk.Checked)
74 //取出选中行的主键,加入到集合中
75 ids.Add((int)GridView1.DataKeys[gvr.RowIndex].Value);
76 }
77 }
78 return ids;
79 }
80 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
81 {
82 ClientInfoAccessObj accessor = new ClientInfoAccessObj();
83 GridView1.DataSource = accessor.GetAllClients();
84 GridView1.PageIndex = e.NewPageIndex;
85 GridView1.DataBind();
86 //保存当前页码
87 curPage = e.NewPageIndex;
88 }
89}
90
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 //当前页码,从0开始
16 private int curPage
17 {
18 get
19 {
20 return ViewState["curPage"] == null ? 0 : Convert.ToInt32(ViewState["curPage"]);
21 }
22 set
23 {
24 ViewState["curPage"] = value;
25 }
26 }
27 protected void Page_Load(object sender, EventArgs e)
28 {
29 if (!IsPostBack)
30 {
31 ClientInfoAccessObj accessor = new ClientInfoAccessObj();
32 curPage = 0; //显示第一页
33 GridView1.DataSource = accessor.GetAllClients();
34 GridView1.DataBind();
35 }
36 }
37 protected void btnDeleteSelectedInfo_Click(object sender, EventArgs e)
38 {
39 List<int> SelectedClientIDs = GetSelectedClientIDs();
40 if (SelectedClientIDs.Count == 0)
41 return;
42
43 ClientInfoAccessObj accessor = new ClientInfoAccessObj();
44 foreach (int id in SelectedClientIDs)
45 {
46 accessor.DeleteClientInfoForID(id);
47 }
48 ClientScript.RegisterStartupScript(this.GetType(), "info", "alert('记录已成功删除');", true);
49
50 //计算应该显示哪一页
51 if (SelectedClientIDs.Count == GridView1.PageSize) //选中全页上的所有记录
52 if (curPage != 0)
53 curPage--; //显示前一页
54
55
56 //重新绑定数据并显示
57 GridView1.DataSource = accessor.GetAllClients();
58 GridView1.PageIndex = curPage;
59 GridView1.DataBind();
60 }
61
62 private List<int> GetSelectedClientIDs()
63 {
64 List<int> ids = new List<int>();
65 //循环查找被选中的行
66 foreach (GridViewRow gvr in GridView1.Rows)
67 {
68 //是数据行
69 if (gvr.RowType == DataControlRowType.DataRow)
70 {
71 //根据模板列中的控件ID查找指定的控件
72 CheckBox chk = gvr.FindControl("CheckBox1") as CheckBox;
73 if ((chk != null) && chk.Checked)
74 //取出选中行的主键,加入到集合中
75 ids.Add((int)GridView1.DataKeys[gvr.RowIndex].Value);
76 }
77 }
78 return ids;
79 }
80 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
81 {
82 ClientInfoAccessObj accessor = new ClientInfoAccessObj();
83 GridView1.DataSource = accessor.GetAllClients();
84 GridView1.PageIndex = e.NewPageIndex;
85 GridView1.DataBind();
86 //保存当前页码
87 curPage = e.NewPageIndex;
88 }
89}
90