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

2

3

4

5

6

7

8

9

10



11


12

13

14

15



16


17

18

19

20

21


22

23



24

25



26

27

28

29

30



31

32

33

34

35

36

37

38

39



40

41

42

43

44

45


46

47

48



49

50



51

52

53

54

55

56

57



58

59



60

61

62

63

64

65

66

67


68

69



70

71

72

73

74

75

76

77

78

79

80



81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

.jpg)