datagrid分頁,排序,跨頁多選。可以分頁後還是排序的。
如果大家測出什麼bug,請留言,大家一起討論。
如果大家有什麼別的好文章請貼出聯接共享。
好的文章待續ing。
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Xml;
12
using System.Xml.XPath;
13
using System.Text;
14
15
namespace WebApplication4
16
{
17
/// <summary>
18
/// WebForm1 的摘要描述。
19
/// </summary>
20
public class WebForm1 : System.Web.UI.Page
21
{
22
protected System.Web.UI.HtmlControls.HtmlInputHidden hidselectindex;
23
protected System.Web.UI.WebControls.Label Label1;
24
protected System.Web.UI.WebControls.Button Button1;
25
protected System.Web.UI.WebControls.Label SortOrder;
26
protected System.Web.UI.WebControls.Label RequestedPage;
27
protected System.Web.UI.WebControls.DataGrid DataGrid1;
28
29
private void Page_Load(object sender, System.EventArgs e)
30
{
31
// 在這裡放置使用者程式碼以初始化網頁
32
if(!Page.IsPostBack)
33
{
34
SortOrder.Text = "Name";
35
this.RequestedPage.Text="0";
36
37
Bind();
38
}
39
select();
40
// add();
41
}
42
43
Web Form 設計工具產生的程式碼
67
private void Bind()
68
{
69
DataSet ds=new DataSet();
70
ds.ReadXml(Server.MapPath("user.xml"));
71
DataView dv=new DataView();
72
dv=ds.Tables[0].DefaultView;
73
this.DataGrid1.DataSource=ds.Tables[0].DefaultView;
74
if(Convert.ToInt32(this.RequestedPage.Text)>=Convert.ToInt32(ds.Tables[0].Rows.Count/this.DataGrid1.PageSize))
75
RequestedPage.Text = "0";
76
dv.Sort=SortOrder.Text;
77
this.DataGrid1.CurrentPageIndex=Convert.ToInt32(this.RequestedPage.Text);
78
this.DataGrid1.DataBind();
79
select();
80
// add();
81
}
82
83
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
84
{
85
this.RequestedPage.Text=e.NewPageIndex.ToString();
86
Bind();
87
}
88
89
90
private void select()
91
{
92
foreach ( DataGridItem item in this.DataGrid1.Controls[0].Controls)
93
{
94
if (item.ItemType == ListItemType.Header)
95
{
96
97
CheckBox CheckBox1=(CheckBox)item.FindControl("CheckBox1");
98
System.Text.StringBuilder strscript = new System.Text.StringBuilder("<script language='javascript'> \n");
99
strscript.Append(" function checkstatus() { \n");
100
strscript.Append(" var ball = true; \n");
101
strscript.Append(" ball = document.all('" + CheckBox1.ClientID + "').checked; \n");
102
103
for(int i=0; i<this.DataGrid1.Items.Count ; i++)
104
{
105
strscript.Append(" document.all('" + ((HtmlInputCheckBox)(this.DataGrid1.Items[i].Cells[0].FindControl("CheckBox2"))).ClientID+ "').checked = ball; \n");
106
strscript.Append(" AddRemoveValues(document.all('" + ((HtmlInputCheckBox)(this.DataGrid1.Items[i].Cells[0].FindControl("CheckBox2"))).ClientID+ "')); \n");
107
}
108
strscript.Append(" } \n");
109
strscript.Append("</script> \n");
110
if(!Page.IsClientScriptBlockRegistered("checkstatus"))
111
Page.RegisterClientScriptBlock("checkstatus",strscript.ToString());
112
CheckBox1.Attributes.Add("onclick","checkstatus()");
113
return;
114
}
115
}
116
}
117
118
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
119
{
120
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
121
{
122
if(hidselectindex.Value.IndexOf(e.Item.Cells[1].Text) >= 0 )
123
{
124
HtmlInputCheckBox CheckBox2 = (HtmlInputCheckBox)(e.Item.Cells[0].FindControl("CheckBox2"));
125
CheckBox2.Checked = true;
126
}
127
}
128
129
}
130
131
private void Button1_Click(object sender, System.EventArgs e)
132
{
133
Label1.Text = hidselectindex.Value.Replace(",","<li>");
134
135
}
136
137
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
138
{
139
if(SortOrder.Text.Trim()==e.SortExpression.Trim())
140
SortOrder.Text = e.SortExpression.Trim() + " desc";
141
else
142
SortOrder.Text = e.SortExpression.Trim();
143
Bind();
144
145
}
146
147
148
149
}
150
}
151
原代碼:
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

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

/Files/gjcn/WebForm1.rar