LoginControl Bubble Event
Add a Bubble Event
1using System;
2using System.Collections;
3using System.Collections.Specialized;
4using System.ComponentModel;
5using System.Web;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8
9namespace PowerAsp.NET.Comtrols
10{
11 /**//// <summary>
12 /// 复合控件例子。
13 /// </summary>
14 public class LoginControl : System.Web.UI.WebControls.WebControl,INamingContainer
15 {
16 pubic method#region pubic method
17 public static object _loginEvent = new object();
18 #endregion
19
20 pulbic event#region pulbic event
21 public event EventHandler Login
22 {
23 add
24 {
25 Events.AddHandler(_loginEvent,value);
26 }
27 remove
28 {
29 Events.RemoveHandler(_loginEvent,value);
30 }
31 }
32 #endregion
33
34 要复合的控件#region 要复合的控件
35 private TextBox _namecontrol,_passcontrol;
36 private Button _loginBtn;
37 #endregion
38
39 override parent methods#region override parent methods
40 protected override HtmlTextWriterTag TagKey
41 {
42 get
43 {
44 return HtmlTextWriterTag.Table;
45 }
46 }
47 protected override void CreateChildControls()
48 {
49 Controls.Clear();
50 ClearChildViewState();
51 CreateControlHierarchy();
52 PrepareControlHierarchy();
53 TrackViewState();
54 ChildControlsCreated = true;
55 }
56 protected override void Render(HtmlTextWriter writer)
57 {
58 EnsureChildControls();
59 base.Render (writer);
60 }
61 protected override bool OnBubbleEvent(object source, EventArgs args)
62 {
63 if(source is Button)
64 {
65 OnLogin(null);
66 return true;
67 }
68 return base.OnBubbleEvent (source, args);
69 }
70 protected virtual void OnLogin(EventArgs args)
71 {
72 EventHandler loginhandler = (EventHandler)Events[_loginEvent];
73 if(loginhandler != null)
74 {
75 loginhandler(this,null);
76 }
77 else
78 {
79 base.RaiseBubbleEvent(this,args);
80 }
81 }
82 #endregion
83
84 property (not browsable in property)#region property (not browsable in property)
85 [BrowsableAttribute(false)]
86 public string userName
87 {
88 get
89 {
90 EnsureChildControls();
91 return _namecontrol.Text;
92 }
93 }
94 [BrowsableAttribute(false)]
95 public string PassWord
96 {
97 get
98 {
99 EnsureChildControls();
100 return _passcontrol.Text;
101 }
102 }
103
104 #endregion
105
106 private method#region private method
107 protected virtual void CreateControlHierarchy()
108 {
109 _namecontrol = new TextBox();
110 _passcontrol = new TextBox();
111 _loginBtn = new Button();
112 _namecontrol.ID = "UserName";
113 _passcontrol.ID = "PassWord";
114 _loginBtn.ID = "LoginButton";
115 _loginBtn.Text = "OK";
116 ChildControlsCreated = true;
117 }
118 protected virtual void PrepareControlHierarchy()
119 {
120 TableRow row = new TableRow();
121 TableCell cell = new TableCell();
122 row.Controls.Add(cell);
123 cell.Controls.Add(_namecontrol);
124
125 cell = new TableCell();
126 row.Controls.Add(cell);
127
128 Controls.Add(row);
129
130
131 row = new TableRow();
132 cell = new TableCell();
133 row.Controls.Add(cell);
134 cell.Controls.Add(_passcontrol);
135 cell = new TableCell();
136 row.Controls.Add(cell);
137 cell.Controls.Add(_loginBtn);
138 Controls.Add(row);
139 }
140 #endregion
141 }
142}
143
1using System;
2using System.Collections;
3using System.Collections.Specialized;
4using System.ComponentModel;
5using System.Web;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8
9namespace PowerAsp.NET.Comtrols
10{
11 /**//// <summary>
12 /// 复合控件例子。
13 /// </summary>
14 public class LoginControl : System.Web.UI.WebControls.WebControl,INamingContainer
15 {
16 pubic method#region pubic method
17 public static object _loginEvent = new object();
18 #endregion
19
20 pulbic event#region pulbic event
21 public event EventHandler Login
22 {
23 add
24 {
25 Events.AddHandler(_loginEvent,value);
26 }
27 remove
28 {
29 Events.RemoveHandler(_loginEvent,value);
30 }
31 }
32 #endregion
33
34 要复合的控件#region 要复合的控件
35 private TextBox _namecontrol,_passcontrol;
36 private Button _loginBtn;
37 #endregion
38
39 override parent methods#region override parent methods
40 protected override HtmlTextWriterTag TagKey
41 {
42 get
43 {
44 return HtmlTextWriterTag.Table;
45 }
46 }
47 protected override void CreateChildControls()
48 {
49 Controls.Clear();
50 ClearChildViewState();
51 CreateControlHierarchy();
52 PrepareControlHierarchy();
53 TrackViewState();
54 ChildControlsCreated = true;
55 }
56 protected override void Render(HtmlTextWriter writer)
57 {
58 EnsureChildControls();
59 base.Render (writer);
60 }
61 protected override bool OnBubbleEvent(object source, EventArgs args)
62 {
63 if(source is Button)
64 {
65 OnLogin(null);
66 return true;
67 }
68 return base.OnBubbleEvent (source, args);
69 }
70 protected virtual void OnLogin(EventArgs args)
71 {
72 EventHandler loginhandler = (EventHandler)Events[_loginEvent];
73 if(loginhandler != null)
74 {
75 loginhandler(this,null);
76 }
77 else
78 {
79 base.RaiseBubbleEvent(this,args);
80 }
81 }
82 #endregion
83
84 property (not browsable in property)#region property (not browsable in property)
85 [BrowsableAttribute(false)]
86 public string userName
87 {
88 get
89 {
90 EnsureChildControls();
91 return _namecontrol.Text;
92 }
93 }
94 [BrowsableAttribute(false)]
95 public string PassWord
96 {
97 get
98 {
99 EnsureChildControls();
100 return _passcontrol.Text;
101 }
102 }
103
104 #endregion
105
106 private method#region private method
107 protected virtual void CreateControlHierarchy()
108 {
109 _namecontrol = new TextBox();
110 _passcontrol = new TextBox();
111 _loginBtn = new Button();
112 _namecontrol.ID = "UserName";
113 _passcontrol.ID = "PassWord";
114 _loginBtn.ID = "LoginButton";
115 _loginBtn.Text = "OK";
116 ChildControlsCreated = true;
117 }
118 protected virtual void PrepareControlHierarchy()
119 {
120 TableRow row = new TableRow();
121 TableCell cell = new TableCell();
122 row.Controls.Add(cell);
123 cell.Controls.Add(_namecontrol);
124
125 cell = new TableCell();
126 row.Controls.Add(cell);
127
128 Controls.Add(row);
129
130
131 row = new TableRow();
132 cell = new TableCell();
133 row.Controls.Add(cell);
134 cell.Controls.Add(_passcontrol);
135 cell = new TableCell();
136 row.Controls.Add(cell);
137 cell.Controls.Add(_loginBtn);
138 Controls.Add(row);
139 }
140 #endregion
141 }
142}
143