CompositeControl 后篇 --事件冒泡
系列文章链接:
ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件
ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇
ASP.NET自定义控件组件开发 第三章 为控件添加事件 后篇
ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl
ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡
我们上一篇已经开发一个登录的组合控件,但是我们把控件的事件的这个问题没有解决,现在我们就来看看这个
问题。
因为我们的组合控件Login是由一些已经存在的服务器控件的组合的,这样就省却了我们很多的麻烦,上篇中我
们已经把子控件的一些属性,如Text等,显示呈现为了组合控件的属性。我们将之称为“属性的冒泡”(我自己这样认为
的)。
我们现在来想下:其实我们点击“提交”按钮,我们就想把控件中信息发送给服务器,然后验证。即,我们点击
的是“提交”按钮,但是触发的确实Login控件的验证事件。也就是说,我们应该把子控件-button的事件,上升冒泡成为
Login的事件。
现在我们就正式讲讲怎么来事件冒泡。
对于事件的一些步骤,大家应该还记得:
1。定义一个数据类,用来当发生事件时,我们把一些信息携带。在本控件中,当我们点击“提交”按钮时,我
们就把用户名和密码作为数据送到服务器。所以我们的数据类包含两个信息:如下:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 public class ValidateEventArgs:EventArgs
2 {
3 private string userName;
4 private string userPassward;
5
6 public string UserName
7 {
8 get
9 {
10 return userName ;
11 }
12 }
13
14 public string UserPassward
15 {
16 get
17 {
18 return userPassward;
19 }
20 }
21
22 public ValidateEventArgs(string username, string userpassward)
23 {
24 this.userName = username;
25 this.userPassward = userpassward;
26 }
27 }
相信大家对上面的代码不陌生!!!
2.我们来定义一个委托:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 public delegate void ValidateEventHandler(object sender,ValidateEventArgs args);
3.定义事件。
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 private static readonly object ValidateEventKey = new object();
2 public event ValidateEventHandler ValidateUserInfoChanged
3 {
4 add
5 {
6 Events.AddHandler(ValidateEventKey, value);
7 }
8 remove
9 {
10 Events.RemoveHandler(ValidateEventKey, value);
11 }
12 }
13
14 protected virtual void OnValidateChanged(ValidateEventArgs args)
15 {
16 ValidateEventHandler handler = Events[ValidateEventKey] as ValidateEventHandler;
17 if (handler != null)
18 handler(this, args);
19 }
20
4.好了;准备都做好了,下面我们只要把这个事件冒泡成为Login的控件的就行了。其实很简单的。只要重写一个方
法就行了。
如下:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 protected override bool OnBubbleEvent(object source, EventArgs args)
2 {
3 bool handled = false;
4
5 CommandEventArgs ce = args as CommandEventArgs;
6 if (ce != null && ce.CommandName == "Validate")
7 {
8 handled = true;
9 string userName = this.txtUserName.Text;
10 string userPassward = this.txtUserPassward.Text;
11
12 //把信息输入
13 ValidateEventArgs ve = new ValidateEventArgs(userName, userPassward);
14 OnValidateChanged(ve);
15 }
16 return handled;
17 }
好了,完了,就这样了。
完整代码附上:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.ComponentModel;
8
9 namespace LoginControl
10 {
11 public class Login:CompositeControl
12 {
13 #region 要组合的控件
14 Label lbUserName;
15 Label lbUserPassward;
16 TextBox txtUserName;
17 TextBox txtUserPassward;
18 Button submitButton;
19 #endregion
20
21
22 #region 重写方法CreateChildControls
23
24 protected override void CreateChildControls()
25 {
26 Controls.Clear();
27
28 //初始化控件lbUserName
29 lbUserName = new Label();
30 lbUserName.Text = "用户名:";
31 lbUserName.ID = "lbUserName";
32 //把控件添加到我们的组合控件中
33 Controls.Add(lbUserName);
34
35 //初始化控件lbUserPassward
36 lbUserPassward = new Label();
37 lbUserPassward.Text = "密 码:";
38 lbUserPassward.ID = "lbUserPassward";
39 Controls.Add(lbUserPassward);
40
41
42 txtUserName = new TextBox();
43 txtUserName.ID = "txtUserName";
44 txtUserName.Width = Unit.Percentage(60);
45 Controls.Add(txtUserName);
46
47 txtUserPassward = new TextBox();
48 txtUserPassward.ID = "txtUserPassward";
49 txtUserPassward.Width = Unit.Percentage(60);
50 Controls.Add(txtUserPassward);
51
52 submitButton = new Button();
53 submitButton.Text = "提交";
54 submitButton.CommandName = "Validate";
55 Controls.Add(submitButton);
56
57 ChildControlsCreated = true;
58 }
59 #endregion
60 #region 将组合的子控件的属性 呈现为 组合控件的属性
61
62 public string UserNameLabelText
63 {
64 get
65 {
66 EnsureChildControls();
67 return lbUserName.Text;
68 }
69 set
70 {
71 EnsureChildControls();
72 lbUserName.Text = value;
73 }
74 }
75
76 public string UserPasswardLabelText
77 {
78 get
79 {
80 EnsureChildControls();
81 return lbUserPassward.Text;
82 }
83 set
84 {
85 EnsureChildControls();
86 lbUserPassward.Text = value;
87 }
88 }
89
90 public string SubmitButtonText
91 {
92 get
93 {
94 EnsureChildControls();
95 return submitButton.Text;
96 }
97 set
98 {
99 EnsureChildControls();
100 submitButton.Text = value;
101 }
102 }
103
104
105 #endregion
106
107 #region 组合控件呈现的样式
108 protected override HtmlTextWriterTag TagKey
109 {
110 get
111 {
112 return HtmlTextWriterTag.Table;
113 }
114 }
115
116 protected override void RenderContents(HtmlTextWriter writer)
117 {
118
119 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
120
121 writer.RenderBeginTag(HtmlTextWriterTag.Td);
122 lbUserName.RenderControl(writer);
123 writer.RenderEndTag();//td的结束
124
125 writer.RenderBeginTag(HtmlTextWriterTag.Td);
126 txtUserName.RenderControl(writer);
127 writer.RenderEndTag ();
128
129 writer.RenderEndTag ();//tr的结束
130
131 //***********************************************
132
133 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
134
135 writer.RenderBeginTag(HtmlTextWriterTag.Td);
136 lbUserPassward .RenderControl(writer);
137 writer.RenderEndTag();//td的结束
138
139 writer.RenderBeginTag(HtmlTextWriterTag.Td);
140 txtUserPassward .RenderControl(writer);
141 writer.RenderEndTag();
142
143 writer.RenderEndTag ();//tr的结束
144
145 //***********************************************
146
147 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
148
149 writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
150 writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
151 writer.RenderBeginTag(HtmlTextWriterTag.Td);
152 submitButton.RenderControl(writer);
153 writer.RenderEndTag ();
154
155 writer.RenderEndTag();//tr的结束
156
157
158
159
160 }
161 #endregion
162
163 #region 事件
164 private static readonly object ValidateEventKey = new object();
165 public event ValidateEventHandler ValidateUserInfoChanged
166 {
167 add
168 {
169 Events.AddHandler(ValidateEventKey, value);
170 }
171 remove
172 {
173 Events.RemoveHandler(ValidateEventKey, value);
174 }
175 }
176
177 protected virtual void OnValidateChanged(ValidateEventArgs args)
178 {
179 ValidateEventHandler handler = Events[ValidateEventKey] as ValidateEventHandler;
180 if (handler != null)
181 handler(this, args);
182 }
183
184 #endregion
185
186 #region 冒泡
187
188 protected override bool OnBubbleEvent(object source, EventArgs args)
189 {
190 bool handled = false;
191
192 CommandEventArgs ce = args as CommandEventArgs;
193 if (ce != null && ce.CommandName == "Validate")
194 {
195 handled = true;
196 string userName = this.txtUserName.Text;
197 string userPassward = this.txtUserPassward.Text;
198
199 //把信息输入
200 ValidateEventArgs ve = new ValidateEventArgs(userName, userPassward);
201 OnValidateChanged(ve);
202 }
203 return handled;
204 }
205 #endregion
206
207
208
209 }
210 }
211