GridView中动态添加模板列和其子控件集合

一、实施说明:

1 实施原因:

因为在做一个MOSS WebPart控件时候,遇到一个问题,如何选择需要导出的数据列;

GridView的数据源是动态的,所以没有办法静态使用模板列,来选择导出的列;

之所以没有用BoundField的原因,是因为一列数据列中不能放入两个或两个以上的控件,

以及不能在表头加入相关控件。所以,最后,我采用动态添加模板列的方法解决这个问题,

欢迎大家测试和改善。

2、待改善问题:

a、如何控制模板列中子控件集合的UI布局,

b、HeaderTemplate区域不能对子控件进行数据绑定。

如图所示:

选择文件清单“Site_Menu”时候:

选择文件清单“Web 部件库”时候

二、代码结构

类对象的使用关系:

GridVeiw-->Column-->TemplateField-->Header/DataRow-->MyTemplateColumn-->ColumnField;

其中MyTemplateField继承ITemplate;

ControlField为MyTemplateField提供构造模板列的相关数据。

三、代码内容

1、MyTemplateColumn.cs,详情如下:

MyTemplateColumn.cs
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12
13 namespace Test.WebControls.GridView.MyTemplateColumn
14 {
15 public class MyTmeplateColumn:ITemplate
16 {
17 /// <summary>
18 /// 委托
19 /// </summary>
20 /// <param name="sender"></param>
21 /// <param name="e"></param>
22 public delegate void EventHandler(object sender, EventArgs e);
23 /// <summary>
24 /// 事件
25 /// </summary>
26 public event EventHandler eh;
27 /// <summary>
28 /// 模板列的类型
29 /// </summary>
30 private DataControlRowType templateType;
31
32 /// <summary>
33 /// 数据绑定列的标题
34 /// </summary>
35 private string HeaderText;
36 /// <summary>
37 /// 模板列控件对象的集合<控件ID, 控件对象>
38 /// </summary>
39 private System.Collections.Generic.Dictionary<string, ControlField> ControlsFieldList;
40
41 public MyTmeplateColumn(DataControlRowType type, string headerText)
42 {
43 this.templateType = type;
44 this.HeaderText = headerText;
45 }
46
47 public MyTmeplateColumn(DataControlRowType type, string headerText, System.Collections.Generic.Dictionary<string, ControlField> ControlsFieldList)
48 {
49 this.templateType = type;
50 this.HeaderText = headerText;
51 this.ControlsFieldList = ControlsFieldList;
52 }
53
54 /// <summary>
55 /// 进行数据绑定
56 /// </summary>
57 /// <param name="container"></param>
58 public void InstantiateIn(System.Web.UI.Control container)
59 {
60 switch (templateType)
61 {
62 case DataControlRowType.Header:
63 {
64 if (this.ControlsFieldList != null && this.ControlsFieldList.Count > 0)
65 {
66 for (int i = 0; i < this.ControlsFieldList.Count; i++)
67 {
68 string ID = this.ControlsFieldList.Keys.ToArray()[i].ToString().Trim();
69 ControlField controlField = this.ControlsFieldList[ID];
70
71 // 执行数据绑定
72 System.Web.UI.Control control = controlField.UIControl;
73 if (control != null && controlField != null)
74 {
75 //control.DataBinding += new System.EventHandler(ControlFieldDataBinding);
76 container.Controls.Add(control);
77 }
78 }
79 }
80
81 Literal lc = new Literal();
82 lc.Text = this.HeaderText;
83 container.Controls.Add(lc);
84
85 break;
86 }
87 case DataControlRowType.DataRow:
88 {
89
90 if (this.ControlsFieldList != null && this.ControlsFieldList.Count > 0)
91 {
92 for (int i = 0; i < this.ControlsFieldList.Count; i++)
93 {
94 string ID = this.ControlsFieldList.Keys.ToArray()[i].ToString().Trim();
95 ControlField controlField = this.ControlsFieldList[ID];
96
97 // 执行数据绑定
98 System.Web.UI.Control control = controlField.UIControl;
99 if (control != null && controlField != null)
100 {
101 control.DataBinding += new System.EventHandler(ControlFieldDataBinding);
102 container.Controls.Add(control);
103 }
104 }
105 }
106 break;
107 }
108 default:
109 {
110 break;
111 }
112 }
113 }
114
115 /// <summary>
116 /// ControlField的对象进行数据绑定
117 /// </summary>
118 /// <param name="controlField"></param>
119 public void ControlFieldDataBinding(object sender, EventArgs e)
120 {
121 GridViewRow container = null;
122 ControlField controlField = null;
123
124 string control_type = sender.GetType().Name;
125 string control_id = "";
126 switch (control_type)
127 {
128 case "Label":
129 {
130 control_id = (sender as Label).ID.Trim();
131 break;
132 }
133 case "TextBox":
134 {
135 control_id = (sender as TextBox).ID.Trim();
136 break;
137 }
138 case "LinkButton":
139 {
140 control_id = (sender as LinkButton).ID.Trim();
141 break;
142 }
143 case "Button":
144 {
145 control_id = (sender as Button).ID.Trim();
146 break;
147 }
148 case "DropDownList":
149 {
150 control_id = (sender as DropDownList).ID.Trim();
151 break;
152 }
153 case "CheckBox":
154 {
155 control_id = (sender as CheckBox).ID.Trim();
156 break;
157 }
158 case "RadioButton":
159 {
160 control_id = (sender as RadioButton).ID.Trim();
161 break;
162 }
163 }
164 if (control_id != "")
165 {
166 controlField = this.ControlsFieldList[control_id];
167 }
168
169
170 if (controlField != null)
171 {
172 // 转化子控件
173 System.Collections.Generic.Dictionary<string, string> fields = null;// <property, key> = <服务器控件属性名称, 属性绑定数据字段>
174 string property = "";//服务器控件属性名称
175 string key = "";//属性绑定数据字段
176 string value = ""; //临时变量
177 //
178 switch (controlField.ControlType)
179 {
180 #region 按控件类别,进行数据绑定
181 case ControlTypeEnum.Label:
182 {
183 #region Label DataBinding
184 Label obj = controlField.UIControl as Label;
185 container = obj.NamingContainer as GridViewRow;
186
187 if (obj != null && container != null)
188 {
189 if (obj != null && controlField.Fields != null && controlField.Fields.Count > 0)
190 {
191 fields = controlField.Fields;
192 for (int i = 0; i < fields.Count; i++)
193 {
194 property = fields.Keys.ToArray()[i];
195 if (property != null && property.Trim() != "")
196 {
197 key = fields[property];
198 value = DataBinder.Eval(container.DataItem, key).ToString().Trim();
199 switch (property.Trim().ToUpper())
200 {
201 case "TEXT":
202 {
203 obj.Text = value;
204 break;
205 }
206 case "TOOLTIP":
207 {
208 obj.ToolTip = value;
209 break;
210 }
211 case "ENABLED":
212 {
213 obj.Enabled = Convert.ToBoolean(value);
214 break;
215 }
216 case "VISIBLE":
217 {
218 obj.Visible = Convert.ToBoolean(value);
219 break;
220 }
221 }
222 }
223 }
224 }
225 }
226 break;
227 #endregion
228 }
229 case ControlTypeEnum.TextBox:
230 {
231 #region TextBox DataBinding
232 TextBox obj = controlField.UIControl as TextBox; //sender as TextBox;
233 container = obj.NamingContainer as GridViewRow;
234
235 if (obj != null && container != null)
236 {
237 if (obj != null && controlField.Fields != null && controlField.Fields.Count > 0)
238 {
239 fields = controlField.Fields;
240 for (int i = 0; i < fields.Count; i++)
241 {
242 property = fields.Keys.ToArray()[i];
243 if (property != null && property.Trim() != "")
244 {
245 key = fields[property];
246 value = DataBinder.Eval(container.DataItem, key).ToString().Trim();
247 switch (property.Trim().ToUpper())
248 {
249 case "TEXT":
250 {
251 obj.Text = value;
252 break;
253 }
254 case "TOOLTIP":
255 {
256 obj.ToolTip = value;
257 break;
258 }
259 case "ENABLED":
260 {
261 obj.Enabled = Convert.ToBoolean(value);
262 break;
263 }
264 case "VISIBLE":
265 {
266 obj.Visible = Convert.ToBoolean(value);
267 break;
268 }
269 }
270 }
271 }
272 }
273 }
274 break;
275 #endregion
276 }
277 case ControlTypeEnum.LinkButton:
278 {
279 #region LinkButton DataBinding
280 LinkButton obj = controlField.UIControl as LinkButton;
281 container = obj.NamingContainer as GridViewRow;
282
283 if (obj != null && container != null)
284 {
285 if (obj != null && controlField.Fields != null && controlField.Fields.Count > 0)
286 {
287 fields = controlField.Fields;
288 for (int i = 0; i < fields.Count; i++)
289 {
290 property = fields.Keys.ToArray()[i];
291 if (property != null && property.Trim() != "")
292 {
293 key = fields[property];
294 value = DataBinder.Eval(container.DataItem, key).ToString().Trim();
295 switch (property.Trim().ToUpper())
296 {
297 case "TEXT":
298 {
299 obj.Text = value;
300 break;
301 }
302 case "TOOLTIP":
303 {
304 obj.ToolTip = value;
305 break;
306 }
307 case "ENABLED":
308 {
309 obj.Enabled = Convert.ToBoolean(value);
310 break;
311 }
312 case "VISIBLE":
313 {
314 obj.Visible = Convert.ToBoolean(value);
315 break;
316 }
317 case "POSTBACKURL":
318 {
319 obj.PostBackUrl = value;
320 break;
321 }
322 }
323 }
324 }
325 }
326 }
327 break;
328 #endregion
329 }
330 case ControlTypeEnum.Button:
331 {
332 #region Button DataBinding
333 Button obj = controlField.UIControl as Button;
334 container = obj.NamingContainer as GridViewRow;
335
336 if (obj != null && container != null)
337 {
338 if (obj != null && controlField.Fields != null && controlField.Fields.Count > 0)
339 {
340 fields = controlField.Fields;
341 for (int i = 0; i < fields.Count; i++)
342 {
343 property = fields.Keys.ToArray()[i];
344 if (property != null && property.Trim() != "")
345 {
346 key = fields[property];
347 value = DataBinder.Eval(container.DataItem, key).ToString().Trim();
348 switch (property.Trim().ToUpper())
349 {
350 case "TEXT":
351 {
352 obj.Text = value;
353 break;
354 }
355 case "TOOLTIP":
356 {
357 obj.ToolTip = value;
358 break;
359 }
360 case "ENABLED":
361 {
362 obj.Enabled = Convert.ToBoolean(value);
363 break;
364 }
365 case "VISIBLE":
366 {
367 obj.Visible = Convert.ToBoolean(value);
368 break;
369 }
370 case "POSTBACKURL":
371 {
372 obj.PostBackUrl = value;
373 break;
374 }
375 }
376 }
377 }
378 }
379 }
380 break;
381 #endregion\
382 }
383 case ControlTypeEnum.DropDownList:
384 {
385
386 break;
387 }
388 case ControlTypeEnum.CheckBox:
389 {
390 #region CheckBox DataBinding
391 CheckBox obj = controlField.UIControl as CheckBox;
392 container = obj.NamingContainer as GridViewRow;
393
394 if (obj != null && container != null)
395 {
396 if (obj != null && controlField.Fields != null && controlField.Fields.Count > 0)
397 {
398 fields = controlField.Fields;
399 for (int i = 0; i < fields.Count; i++)
400 {
401 property = fields.Keys.ToArray()[i];
402 if (property != null && property.Trim() != "")
403 {
404 key = fields[property];
405 value = DataBinder.Eval(container.DataItem, key).ToString().Trim();
406 switch (property.Trim().ToUpper())
407 {
408 case "TEXT":
409 {
410 obj.Text = value;
411 break;
412 }
413 case "TOOLTIP":
414 {
415 obj.ToolTip = value;
416 break;
417 }
418 case "ENABLED":
419 {
420 obj.Enabled = Convert.ToBoolean(value);
421 break;
422 }
423 case "VISIBLE":
424 {
425 obj.Visible = Convert.ToBoolean(value);
426 break;
427 }
428 case "CHECKED":
429 {
430 obj.Checked = Convert.ToBoolean(value);
431 break;
432 }
433 }
434 }
435 }
436 }
437 }
438 break;
439 #endregion
440 }
441 case ControlTypeEnum.RadioButton:
442 {
443 #region RadioButton DataBinding
444 RadioButton obj = controlField.UIControl as RadioButton;
445 container = obj.NamingContainer as GridViewRow;
446
447 if (obj != null && container != null)
448 {
449 if (obj != null && controlField.Fields != null && controlField.Fields.Count > 0)
450 {
451 fields = controlField.Fields;
452 for (int i = 0; i < fields.Count; i++)
453 {
454 property = fields.Keys.ToArray()[i];
455 if (property != null && property.Trim() != "")
456 {
457 key = fields[property];
458 value = DataBinder.Eval(container.DataItem, key).ToString().Trim();
459 switch (property.Trim().ToUpper())
460 {
461 case "TEXT":
462 {
463 obj.Text = value;
464 break;
465 }
466 case "TOOLTIP":
467 {
468 obj.ToolTip = value;
469 break;
470 }
471 case "ENABLED":
472 {
473 obj.Enabled = Convert.ToBoolean(value);
474 break;
475 }
476 case "VISIBLE":
477 {
478 obj.Visible = Convert.ToBoolean(value);
479 break;
480 }
481 case "CHECKED":
482 {
483 obj.Checked = Convert.ToBoolean(value);
484 break;
485 }
486 }
487 }
488 }
489 }
490 }
491 break;
492 #endregion
493 }
494 #endregion
495 }
496 }
497
498 }
499
500 }
501 }
502

2、ControlField.cs代码详情:

ControlField.cs代码详情
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12
13 namespace Test.WebControls.GridView.MyTemplateColumn
14 {
15 /// <summary>
16 /// 控件类型
17 /// </summary>
18 public enum ControlTypeEnum
19 {
20 Label,
21
22 TextBox,
23
24 LinkButton,
25
26 DropDownList,
27
28 Button,
29
30 CheckBox,
31
32 RadioButton
33 }
34
35 /// <summary>
36 ///
37 /// </summary>
38 public class ControlField
39 {
40 /// <summary>
41 /// 服务器控件类型
42 /// </summary>
43 public ControlTypeEnum ControlType
44 {
45 get;
46 set;
47 }
48
49 /// <summary>
50 /// 服务器控件对象
51 /// </summary>
52 public System.Web.UI.Control UIControl
53 {
54 get;
55 set;
56 }
57
58 /// <summary>
59 /// <服务器控件属性名称, 属性绑定数据字段>
60 /// </summary>
61 public System.Collections.Generic.Dictionary<string, string> Fields
62 {
63 get;
64 set;
65 }
66
67 public ControlField(ControlTypeEnum controlType, System.Web.UI.Control control, System.Collections.Generic.Dictionary<string, string> fileds)
68 {
69 this.ControlType = controlType;
70 this.UIControl = control;
71 this.Fields = fileds;
72 }
73 }
74 }
75

3、 动态添加模板列的代码

动态添加模板列的代码
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13
14 namespace Test.WebControls.GridView.MyTemplateColumn
15 {
16 public partial class test01 : System.Web.UI.Page
17 {
18 protected override void OnInit(EventArgs e)
19 {
20 this.GeneralCols();
21 base.OnInit(e);
22 }
23
24 protected void Page_Load(object sender, EventArgs e)
25 {
26 if (!IsPostBack)
27 {
28 this.Bind();
29 }
30 }
31
32 private void GeneralCols()
33 {
34 // 第1个模板列: Label
35 // HeaderTemplate Row
36 System.Collections.Generic.Dictionary<string, ControlField> controlFieldList1 = new System.Collections.Generic.Dictionary<string, ControlField>();
37 TemplateField col1 = new TemplateField();
38 col1.ShowHeader = true;
39 // CheckBox
40 System.Web.UI.WebControls.CheckBox chk = new CheckBox();
41 chk.ID = "chk";
42 //
43 System.Collections.Generic.Dictionary<string, string> fields0 = new System.Collections.Generic.Dictionary<string, string>();
44 fields0.Add("Checked", "isYes");
45 //
46 ControlField controlField0 = new ControlField(ControlTypeEnum.CheckBox, chk, fields0);
47 controlFieldList1.Add(chk.ID, controlField0);
48 //
49 MyTmeplateColumn col1_headerow = new MyTmeplateColumn(DataControlRowType.Header, "学生编号", controlFieldList1);
50 col1.HeaderTemplate = col1_headerow;
51
52 // ItemTemplate Row
53 System.Collections.Generic.Dictionary<string, ControlField> controlFieldList2 = new System.Collections.Generic.Dictionary<string, ControlField>();
54 //
55 System.Web.UI.WebControls.Label lblID = new Label();
56 lblID.ID = "lblID";
57 //
58 System.Collections.Generic.Dictionary<string, string> fields2 = new System.Collections.Generic.Dictionary<string, string>();
59 fields2.Add("Text", "ID");
60 //
61 ControlField controlField1 = new ControlField(ControlTypeEnum.Label, lblID, fields2);
62 controlFieldList2.Add(lblID.ID, controlField1);
63 //
64 // TextBox
65 System.Web.UI.WebControls.TextBox txtName = new TextBox();
66 txtName.ID = "txtName";
67 //
68 System.Collections.Generic.Dictionary<string, string> fields3 = new System.Collections.Generic.Dictionary<string, string>();
69 fields3.Add("Text", "Name");
70 //
71 ControlField controlField2 = new ControlField(ControlTypeEnum.TextBox, txtName, fields3);
72 controlFieldList2.Add(txtName.ID, controlField2);
73 //
74 MyTmeplateColumn col1_itemrow = new MyTmeplateColumn(DataControlRowType.DataRow, "学生编号", controlFieldList2);
75 col1.ItemTemplate = col1_itemrow;
76
77 gvShow.Columns.Add(col1);
78
79 }
80
81 public void lbtn_Click(object sender, EventArgs e)
82 {
83 ClientScript.RegisterStartupScript(GetType(), "test", "alert('ok');", true);
84 }
85
86
87 private void Bind()
88 {
89 DataTable dt = new DataTable();
90 dt.Columns.Add("id", typeof(string));
91 dt.Columns.Add("name", typeof(string));
92 dt.Columns.Add("isyes", typeof(bool));
93 dt.Columns.Add("url", typeof(string));
94 DataRow dr = dt.NewRow();
95 dr[0] = "No123456";
96 dr[1] = "王晓巍";
97 dr[2] = true;
98 dr[3] = "http://www.baidu.com";
99 dt.Rows.Add(dr);
100 //
101 gvShow.AutoGenerateColumns = false;
102 gvShow.HeaderStyle.BackColor = System.Drawing.Color.BlueViolet;
103 gvShow.HeaderStyle.ForeColor = System.Drawing.Color.FromName("#FFFFFF");
104 gvShow.HeaderStyle.Font.Name = "华文行楷";
105 gvShow.HeaderStyle.Font.Bold = true;
106 gvShow.RowStyle.BackColor = System.Drawing.Color.ForestGreen;
107 gvShow.GridLines = GridLines.Both;
108 gvShow.CellPadding = 3;
109 gvShow.CellSpacing = 0;
110 gvShow.DataSource = dt;
111 gvShow.DataBind();
112 }
113
114 protected void btn_Click(object sender, EventArgs e)
115 {
116 Button btn = sender as Button;
117 if (btn != null)
118 {
119 string s = string.Format("刚才你单击控件{0}: btn.Text={1}",btn.ID, btn.Text);
120 ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + s + "')", true);
121 }
122 }
123 }
124 }
125

4、动态添加模板列的效果图:

posted @ 2010-11-07 13:16  tiandong  阅读(2295)  评论(1编辑  收藏  举报