Itemplate自定义模板列
前些时候写DataGrid合并行效果,在网上偶然必然了这个接口的实现。
感觉挺有意思,不过还没有时间仔细的研究。。先做个标记,回头有时候或有必要的时候在研究下。。。
Itemplate例子Msdn
1using System;
2using System.Web;
3using System.Web.UI;
4
5namespace TemplateControlSamples {
6
7 public class TemplateItem : Control, INamingContainer {
8 private String _message = null;
9
10 public TemplateItem(String message) {
11 _message = message;
12 }
13
14 public String Message {
15
16 get {
17 return _message;
18 }
19 set {
20 _message = value;
21 }
22 }
23 }
24
25 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
26 [ParseChildren(true)]
27 public class Template1 : Control, INamingContainer {
28
29 private ITemplate _messageTemplate = null;
30 private String _message = null;
31
32 public String Message {
33
34 get {
35 return _message;
36 }
37 set {
38 _message = value;
39 }
40 }
41
42 [
43 PersistenceMode(PersistenceMode.InnerProperty),
44 TemplateContainer(typeof(TemplateItem))
45 ]
46 public ITemplate MessageTemplate {
47 get {
48 return _messageTemplate;
49 }
50 set {
51 _messageTemplate = value;
52 }
53 }
54
55 protected override void CreateChildControls() {
56
57 // If a template has been specified, use it to create children.
58 // Otherwise, create a single LiteralControl with the message value.
59
60 if (MessageTemplate != null) {
61 Controls.Clear();
62 TemplateItem i = new TemplateItem(this.Message);
63 MessageTemplate.InstantiateIn(i);
64 Controls.Add(i);
65 }
66 else {
67 this.Controls.Add(new LiteralControl(this.Message));
68 }
69 }
70 }
71}
1using System;
2using System.Web;
3using System.Web.UI;
4
5namespace TemplateControlSamples {
6
7 public class TemplateItem : Control, INamingContainer {
8 private String _message = null;
9
10 public TemplateItem(String message) {
11 _message = message;
12 }
13
14 public String Message {
15
16 get {
17 return _message;
18 }
19 set {
20 _message = value;
21 }
22 }
23 }
24
25 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
26 [ParseChildren(true)]
27 public class Template1 : Control, INamingContainer {
28
29 private ITemplate _messageTemplate = null;
30 private String _message = null;
31
32 public String Message {
33
34 get {
35 return _message;
36 }
37 set {
38 _message = value;
39 }
40 }
41
42 [
43 PersistenceMode(PersistenceMode.InnerProperty),
44 TemplateContainer(typeof(TemplateItem))
45 ]
46 public ITemplate MessageTemplate {
47 get {
48 return _messageTemplate;
49 }
50 set {
51 _messageTemplate = value;
52 }
53 }
54
55 protected override void CreateChildControls() {
56
57 // If a template has been specified, use it to create children.
58 // Otherwise, create a single LiteralControl with the message value.
59
60 if (MessageTemplate != null) {
61 Controls.Clear();
62 TemplateItem i = new TemplateItem(this.Message);
63 MessageTemplate.InstantiateIn(i);
64 Controls.Add(i);
65 }
66 else {
67 this.Controls.Add(new LiteralControl(this.Message));
68 }
69 }
70 }
71}
感觉挺有意思,不过还没有时间仔细的研究。。先做个标记,回头有时候或有必要的时候在研究下。。。