1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Collections.Generic;
12
13
public 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90
