程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> ASP.NET GirdView學習之六 使用模板列實現多行刪除

ASP.NET GirdView學習之六 使用模板列實現多行刪除

編輯:關於ASP.NET
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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved