ASPNetSurvey.ascx:
1
<%@ Control Language="C#" ClassName="ASPNetSurvey" %>
2
3
<script runat="server">
4
5
public bool KnowFast
6
{
7
get
8
{
9
return chkFast.Checked;
10
}
11
}
12
13
public bool KnowNewest
14
{
15
get
16
{
17
return chkNewest.Checked;
18
}
19
}
20
21
</script>
22
23
<asp:CheckBox ID="chkFast" Text="Did you know that ASP.NET is fast?" runat="server" />
24
<br />
25
<br />
26
<asp:CheckBox ID="chkNewest" Text="Did you know that ASP.NET is the newest Microsoft
27
technology for building Web applications?" runat="server" />
28
<br />
29
<br />
30
31
32
上面这个界面有两个属性,获取页面中Checkbox的选定状态,下面一个同样: 
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

ASPSurvey.ascx: <%@ Control Language="C#" ClassName="ASPSurvey" %>
1
<%@ Control Language="C#" ClassName="ASPSurvey" %>
2
3
<script runat="server">
4
5
public bool KnowSlow
6
{
7
get
8
{
9
return chkSlow.Checked;
10
}
11
}
12
13
public bool KnowOutdated
14
{
15
get
16
{
17
return chkOutdated.Checked;
18
}
19
}
20
21
</script>
22
23
<asp:CheckBox ID="chkSlow" Text="Did you know that ASP Classic is slow?" runat="server" />
24
<br />
25
<br />
26
<asp:CheckBox ID="chkOutdated" Text="Did you know that ASP Classic is outdated?"
27
runat="server" />
28
<br />
29
<br />
30
31
下面这一个是动态调用这两个控件的代码(源与后台代码在同一个界面): 
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

WebSurvey.aspx:
1
<%@ Page Language="C#" %>
2
3
<%@ Reference Control="~/ASPSurvey.ascx" %>
4
<%@ Reference Control="~/ASPNetSurvey.ascx" %>
5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
6
7
<script runat="server">
8
9
private Control _survey = null;
10
11
void Page_Load()
12
{
13
switch(ddlLanguage.SelectedIndex)
14
{
15
case 1:
16
_survey = Page.LoadControl("ASPSurvey.ascx");
17
break;
18
case 2:
19
_survey = Page.LoadControl("ASPNetSurvey.ascx");
20
break;
21
}
22
23
if(_survey != null)
24
PlaceHolder1.Controls.Add(_survey);
25
}
26
27
protected void btnSubmit_Click(object sender ,EventArgs e)
28
{
29
switch(ddlLanguage.SelectedIndex)
30
{
31
case 1:
32
ASPSurvey aspResults = (ASPSurvey)_survey;
33
ltlResults.Text = "<h1>ASP Survey</h1>";
34
ltlResults.Text += "<br />Know slow? " + aspResults.KnowSlow.ToString();
35
ltlResults.Text += "<br />Know outdated? " + aspResults.KnowOutdated.ToString();
36
break;
37
case 2:
38
ASPNetSurvey aspNetResults = (ASPNetSurvey)_survey;
39
ltlResults.Text = "<h1>ASP.NET Survey</h1>";
40
ltlResults.Text += "<br />Know fast? " + aspNetResults.KnowFast.ToString();
41
ltlResults.Text += "<br />Know newest? " + aspNetResults.KnowNewest.ToString();
42
break;
43
}
44
}
45
</script>
46
47
<html xmlns="http://www.w3.org/1999/xhtml">
48
<head id="Head1" runat="server">
49
<style type="text/css">
50
html
51
{
52
font:14px Arial,Sans-Serif;
53
}
54
</style>
55
<title>Web Survey</title>
56
</head>
57
<body>
58
<form id="form1" runat="server">
59
<div>
60
<asp:Label ID="lblLanguage" Text="What language do you use to develop Web applications?"
61
runat="server" />
62
<br />
63
<asp:DropDownList ID="ddlLanguage" ToolTip="Web application language (reloads form)"
64
AutoPostBack="true" runat="server">
65
<asp:ListItem Text="Select Language" />
66
<asp:ListItem Text="ASP Classic" />
67
<asp:ListItem Text="ASP.NET" />
68
</asp:DropDownList>
69
<br />
70
<br />
71
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
72
<asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server" />
73
<hr />
74
<asp:Literal ID="ltlResults" runat="server" />
75
</div>
76
</form>
77
</body>
78
</html>
79
80
81
上面这个页面的代码,有很多地方需要提出来一下:
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

第一:对于加载的动态控件,如果包含自定义的属性和方法,那么在引用的时候必须使用<%@Reference%>,这里就有个区别了,如果是直接加载一个没有添加自定义成分的用户控件,则之需要用Page.Load()方法即可,一旦有自定义的成分(属性,方法,事件。。)就需要引用上面的指令了。如果不是动态添加的话,我们一般是使用<%@ Register %>指令<%@Reference%>,这两个怎么区分理解呢:前一个指令 是“引用”,毕竟这个控件没用放到这个页面临时加载而已,故只需要引用就够了,对于不是动态的我们用后一个指令“注册”,这样的指令可以理解为:我需要把一个控件拖到我的页面,贮存在我的页面,需要长时间存在了(除非你删除),那么我就需要注册了,这其实跟生活中的例子一样
第二,用户控件都是控件类,都是在System.Web.UI下的控件类,这里在引用ascx文件的控件类的属性时,只需要先实例化引用的用户控件类,在获取类中的属性。运行效果如下: