古董:实现跨页选择的datagrid
忘了是什么时候写的东西,最近作项目需要又翻了出来,稍微改了一下,先这么用了,呵呵
感兴趣的就拿去吧
必须要设置DataKeyField,因为保存的Key信息就是DataKeyField指定的字段的值,ValueIndex是另一个可以获取的值在dg里的列数
其他和普通的datagrid相同,需要得到选取的值就用SelectedKey和SelectedValue,它返回一个ArrayList,里面是string类型的数据
如要设置checkbox在表格中的位置,请使用CheckBoxIndex,默认是0,即第一列
感兴趣的就拿去吧
必须要设置DataKeyField,因为保存的Key信息就是DataKeyField指定的字段的值,ValueIndex是另一个可以获取的值在dg里的列数
其他和普通的datagrid相同,需要得到选取的值就用SelectedKey和SelectedValue,它返回一个ArrayList,里面是string类型的数据
如要设置checkbox在表格中的位置,请使用CheckBoxIndex,默认是0,即第一列
1
using System;
2
using System.Web.UI;
3
using System.Web.UI.WebControls;
4
using System.Text;
5
using System.Data;
6
using System.Collections;
7
using System.ComponentModel;
8
9
[assembly: TagPrefix("Astrophel","red")]
10
11
namespace Notus
12
{
13
/// <summary>
14
/// 实现跨页选择的datagrid
15
/// </summary>
16
[DefaultProperty("CheckBoxIndex"),ToolboxData("<{0}:SuperGrid Runat=\"server\" CheckboxIndex=\"0\"></{0}:SuperGrid>")]
17
public class SuperGrid : DataGrid
18
{
19
#region 重写 : OnInit,OnPageIndexChanged,OnLoad
20
21
//初始化DataGrid,插入自定义的带Checkbox的模板
22
protected override void OnInit(EventArgs e)
23
{
24
addCheckbox();
25
base.OnInit (e);
26
}
27
28
//设定模板列中Checkbox的Checked属性的值
29
public override void DataBind()
30
{
31
32
base.DataBind ();
33
this.setGrid(this);
34
}
35
36
37
//保存选择的值
38
//这里保存的实际上是上一页选择的值,CurrentPageIndex的值为前一页值
39
protected override void OnLoad(EventArgs e)
40
{
41
this.saveMsg(this);
42
base.OnLoad (e);
43
}
44
45
#endregion
46
47

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

#region 属性 : CheckBoxIndex,SelectedMessage
48
49
//CheckBox插入列的索引
50
private int checkBoxIndex=0;
51
52
[DefaultValue(0),Description("CheckBox插入列的索引")]
53
public int CheckBoxIndex
54
{
55
set{ this.checkBoxIndex=value; }
56
get{return this.checkBoxIndex; }
57
}
58
59
//选择的值
60
[Browsable(false)]
61
public ArrayList SelectedValue
62
{
63
get{ return getMsg(); }
64
65
}
66
67
//选择的键
68
[Browsable(false)]
69
public ArrayList SelectedKey
70
{
71
get{ return getKey(); }
72
73
}
74
75
public string Result
76
{
77
get
78
{
79
System.Text.StringBuilder sb=new System.Text.StringBuilder();
80
ArrayList msg=this.SelectedValue;
81
ArrayList key=this.SelectedKey;
82
for(int i=0;i<msg.Count;i++)
83
{
84
sb.Append(key[i].ToString());
85
sb.Append(". ");
86
sb.Append(msg[i].ToString());
87
sb.Append("<br>");
88
}
89
return sb.ToString();
90
}
91
}
92
93
private int vi;
94
95
[DefaultValue(1),Description("要获取的值的存放索引")]
96
public int ValueIndex
97
{
98
set{this.vi=value;}
99
get{return this.vi;}
100
}
101
102
#endregion
103
104
#region 加入一有CheckBox的模板 : addCheckbox
105
106
/// <summary>
107
/// 加入一有ckeckbox的模板
108
/// </summary>
109
private void addCheckbox()
110
{
111
TemplateColumn tc=new TemplateColumn();
112
tc.ItemTemplate=new CheckboxTemplate();
113
this.Columns.AddAt(checkBoxIndex,tc);
114
}
115
116
#endregion
117
118
#region 具体操作 : saveMsg,setGrid
119
120
/// <summary>
121
/// 选出Session中该页的被选记录的值,根据这些值来决定CheckBox的Checked属性的值
122
/// </summary>
123
private void setGrid(DataGrid dg)
124
{
125
this.CheckSession();
126
127
DataTable dt=(DataTable)Context.Session["SuperGridMsg"];
128
DataRow[] rows=dt.Select("pageIndex="+dg.CurrentPageIndex);
129
130
foreach(DataRow row in rows)
131
{
132
CheckBox cb=(CheckBox)dg.Items[(int)row["itemIndex"]].Cells[checkBoxIndex].FindControl("cb");
133
cb.Checked=true;
134
}
135
136
dt.AcceptChanges();
137
138
}
139
140
/// <summary>
141
/// 在Session中保存该页选中的记录
142
/// </summary>
143
/// 考虑到用户可能回来回翻页并修改选项,这个算法是我所能想出来的最好一个
144
/// 先把该页以前的记录删除,然后再插入新选择的记录
145
private void saveMsg(DataGrid dg)
146
{
147
this.CheckSession();
148
149
DataTable dt=(DataTable)Context.Session["SuperGridMsg"];
150
DataRow[] rows=dt.Select("pageIndex="+dg.CurrentPageIndex);
151
152
foreach(DataRow row in rows)
153
{
154
row.Delete();
155
}
156
foreach(DataGridItem item in dg.Items)
157
{
158
CheckBox cb=(CheckBox)item.Cells[checkBoxIndex].FindControl("cb");
159
if(cb.Checked)
160
{
161
int itemIndex=item.ItemIndex;
162
dt.Rows.Add(new object[]{dg.CurrentPageIndex,itemIndex,dg.DataKeys[itemIndex],item.Cells[vi].Text});
163
}
164
}
165
166
dt.AcceptChanges();
167
}
168
169
#endregion
170
171
#region 得到选择的值 : getMsg
172
173
/// <summary>
174
/// 获得session中现存的值
175
/// </summary>
176
private ArrayList getMsg()
177
{
178
this.CheckSession();
179
this.saveMsg(this);
180
ArrayList msg=new ArrayList();
181
DataTable dt=(DataTable)Context.Session["SuperGridMsg"];
182
foreach(DataRow row in dt.Rows)
183
{
184
msg.Add(row[3].ToString());
185
}
186
187
return msg;
188
}
189
190
#endregion
191
192
#region 得到选择的键 : getKey
193
194
/// <summary>
195
/// 获得session中现存的值
196
/// </summary>
197
private ArrayList getKey()
198
{
199
this.CheckSession();
200
this.saveMsg(this);
201
ArrayList msg=new ArrayList();
202
DataTable dt=(DataTable)Context.Session["SuperGridMsg"];
203
foreach(DataRow row in dt.Rows)
204
{
205
msg.Add(row[2].ToString());
206
}
207
208
return msg;
209
}
210
211
#endregion
212
213
#region 清除session中的记录
214
215
public void ClearSession()
216
{
217
Context.Session["SuperGridMsg"]=null;
218
Context.Session.Remove("SuperGridMsg");
219
}
220
#endregion
221
222
#region 辅助部分 : CheckboxTemplate,CheckSession
223
224
/// <summary>
225
/// 带有CheckBox的模板类
226
/// </summary>
227
public class CheckboxTemplate : ITemplate
228
{
229
public void InstantiateIn(Control container)
230
{
231
CheckBox cb=new CheckBox();
232
cb.ID="cb";
233
container.Controls.Add(cb);
234
}
235
}
236
237
/// <summary>
238
/// 检查session是否存在
239
/// </summary>
240
private void CheckSession()
241
{
242
if(Context.Session["SuperGridMsg"]==null)
243
{
244
DataTable dt=new DataTable();
245
dt.Columns.Add("pageIndex",typeof(int));
246
dt.Columns.Add("itemIndex",typeof(int));
247
dt.Columns.Add("ID");
248
dt.Columns.Add("Value");
249
Context.Session["SuperGridMsg"]=dt;
250
}
251
}
252
#endregion
253
254
}
255
}
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

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

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

1
<notus:supergrid id="list" runat="server" AutoGenerateColumns="False" DataKeyField="NengliID" CheckboxIndex="0"
2
ValueIndex="1">
3
<Columns>
4
<asp:BoundColumn DataField="MingZi" HeaderText="名称"></asp:BoundColumn>
5
</Columns>
6
</notus:supergrid>

2

3

4

5

6

1
#region 绑定数据
2
private void bind(int pageIndex)
3
{
4
BLL.AdminZhiYe az=new CZhiYe.BLL.AdminZhiYe();
5
int count=0;
6
this.list.CurrentPageIndex=pageIndex;
7
this.list.DataSource=(az.GetNengLiSplit(pageIndex,ref count)).GetAll();
8
this.list.DataBind();
9
pages.RecordCount=count;
10
pages.CurrentPageIndex=pageIndex;
11
pages.PageSize=Params.Config.PageSize;
12
13
this.val.Text=this.list.Result;
14
}
15
#endregion

2

3

4

5

6

7

8

9

10

11

12

13

14

15
