LoginControl
一个简单的复合控件的例子,从中尝到不少。。

LoginControl
1
using System;
2
using System.Collections;
3
using System.Collections.Specialized;
4
using System.ComponentModel;
5
using System.Web;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
9
namespace PowerAsp.NET.Comtrols
10

{
11
/**//// <summary>
12
/// 复合控件例子。
13
/// </summary>
14
public class LoginControl : System.Web.UI.WebControls.WebControl,INamingContainer
15
{
16
要复合的控件#region 要复合的控件
17
private TextBox _namecontrol,_passcontrol;
18
private Button _loginBtn;
19
#endregion
20
21
override parent methods#region override parent methods
22
protected override HtmlTextWriterTag TagKey
23
{
24
get
25
{
26
return HtmlTextWriterTag.Table;
27
}
28
}
29
protected override void CreateChildControls()
30
{
31
Controls.Clear();
32
ClearChildViewState();
33
CreateControlHierarchy();
34
PrepareControlHierarchy();
35
TrackViewState();
36
ChildControlsCreated = true;
37
}
38
protected override void Render(HtmlTextWriter writer)
39
{
40
EnsureChildControls();
41
base.Render (writer);
42
}
43
#endregion
44
45
property (not browsable in property)#region property (not browsable in property)
46
[BrowsableAttribute(false)]
47
public string userName
48
{
49
get
50
{
51
EnsureChildControls();
52
return _namecontrol.Text;
53
}
54
}
55
[BrowsableAttribute(false)]
56
public string PassWord
57
{
58
get
59
{
60
EnsureChildControls();
61
return _passcontrol.Text;
62
}
63
}
64
65
#endregion
66
67
private method#region private method
68
protected virtual void CreateControlHierarchy()
69
{
70
_namecontrol = new TextBox();
71
_passcontrol = new TextBox();
72
_loginBtn = new Button();
73
_namecontrol.ID = "UserName";
74
_passcontrol.ID = "PassWord";
75
_loginBtn.ID = "LoginButton";
76
_loginBtn.Text = "OK";
77
ChildControlsCreated = true;
78
}
79
protected virtual void PrepareControlHierarchy()
80
{
81
TableRow row = new TableRow();
82
TableCell cell = new TableCell();
83
row.Controls.Add(cell);
84
cell.Controls.Add(_namecontrol);
85
86
cell = new TableCell();
87
row.Controls.Add(cell);
88
89
Controls.Add(row);
90
91
92
row = new TableRow();
93
cell = new TableCell();
94
row.Controls.Add(cell);
95
cell.Controls.Add(_passcontrol);
96
cell = new TableCell();
97
row.Controls.Add(cell);
98
cell.Controls.Add(_loginBtn);
99
Controls.Add(row);
100
}
101
#endregion
102
}
103
}
104
1
using System;2
using System.Collections;3
using System.Collections.Specialized;4
using System.ComponentModel;5
using System.Web;6
using System.Web.UI;7
using System.Web.UI.WebControls;8

9
namespace PowerAsp.NET.Comtrols10


{11

/**//// <summary>12
/// 复合控件例子。13
/// </summary> 14
public class LoginControl : System.Web.UI.WebControls.WebControl,INamingContainer15

{16

要复合的控件#region 要复合的控件17
private TextBox _namecontrol,_passcontrol;18
private Button _loginBtn;19
#endregion20

21

override parent methods#region override parent methods22
protected override HtmlTextWriterTag TagKey23

{24
get25

{26
return HtmlTextWriterTag.Table;27
}28
}29
protected override void CreateChildControls()30

{31
Controls.Clear();32
ClearChildViewState();33
CreateControlHierarchy();34
PrepareControlHierarchy();35
TrackViewState();36
ChildControlsCreated = true;37
}38
protected override void Render(HtmlTextWriter writer)39

{40
EnsureChildControls();41
base.Render (writer);42
} 43
#endregion44

45

property (not browsable in property)#region property (not browsable in property)46
[BrowsableAttribute(false)]47
public string userName 48

{49
get50

{51
EnsureChildControls();52
return _namecontrol.Text;53
}54
}55
[BrowsableAttribute(false)]56
public string PassWord57

{58
get59

{60
EnsureChildControls();61
return _passcontrol.Text; 62
}63
}64

65
#endregion66

67

private method#region private method68
protected virtual void CreateControlHierarchy()69

{70
_namecontrol = new TextBox();71
_passcontrol = new TextBox();72
_loginBtn = new Button();73
_namecontrol.ID = "UserName";74
_passcontrol.ID = "PassWord";75
_loginBtn.ID = "LoginButton";76
_loginBtn.Text = "OK";77
ChildControlsCreated = true;78
}79
protected virtual void PrepareControlHierarchy()80

{81
TableRow row = new TableRow();82
TableCell cell = new TableCell();83
row.Controls.Add(cell);84
cell.Controls.Add(_namecontrol);85

86
cell = new TableCell();87
row.Controls.Add(cell);88

89
Controls.Add(row);90

91

92
row = new TableRow();93
cell = new TableCell();94
row.Controls.Add(cell);95
cell.Controls.Add(_passcontrol);96
cell = new TableCell();97
row.Controls.Add(cell);98
cell.Controls.Add(_loginBtn);99
Controls.Add(row);100
}101
#endregion 102
}103
}104


浙公网安备 33010602011771号