Preferred Language:
Listing 7.15 - WebSurvey.aspx
Illustrates how to use the reference directive.
<%@ Page Language="C#" %>
<%@ Reference Control="ASPSurvey.ascx" %>
<%@ Reference Control="ASPNetSurvey.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
private Control _survey = null;
void Page_Load()
{
switch (ddlLanguage.SelectedIndex)
{
case 1:
_survey = Page.LoadControl("ASPSurvey.ascx");
break;
case 2:
_survey = Page.LoadControl("ASPNetSurvey.ascx");
break;
}
if (_survey != null)
PlaceHolder1.Controls.Add(_survey);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
switch (ddlLanguage.SelectedIndex)
{
case 1:
ASPSurvey aspResults = (ASPSurvey)_survey;
ltlResults.Text = "<h1>ASP Survey</h1>";
ltlResults.Text += "<br />Know slow? " + aspResults.KnowSlow.ToString();
ltlResults.Text += "<br />Know outdated? " + aspResults.KnowOutdated.ToString();
break;
case 2:
ASPNetSurvey aspNetResults = (ASPNetSurvey)_survey;
ltlResults.Text = "<h1>ASP.NET Survey</h1>";
ltlResults.Text += "<br />Know fast? " + aspNetResults.KnowFast.ToString();
ltlResults.Text += "<br />Know newest? " + aspNetResults.KnowNewest.ToString();
break;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<style type="text/css">
html
{
font:14px Arial,Sans-Serif;
}
</style>
<title>Web Survey</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
id="lblLanguage"
Text="What language do you use to develop Web applications?"
Runat="server" />
<br />
<asp:DropDownList
id="ddlLanguage"
ToolTip="Web application language (reloads form)"
AutoPostBack="true"
Runat="server">
<asp:ListItem Text="Select Language" />
<asp:ListItem Text="ASP Classic" />
<asp:ListItem Text="ASP.NET" />
</asp:DropDownList>
<br /><br />
<asp:PlaceHolder
id="PlaceHolder1"
Runat="server" />
<asp:Button
id="btnSubmit"
Text="Submit"
OnClick="btnSubmit_Click"
Runat="server" />
<hr />
<asp:Literal
id="ltlResults"
Runat="server" />
</div>
</form>
</body>
</html>
The WebSurvey.aspx page references the following user control.
<%@ Control Language="C#" ClassName="ASPSurvey" %>
<script runat="server">
public bool KnowSlow
{
get { return chkSlow.Checked; }
}
public bool KnowOutdated
{
get { return chkOutdated.Checked; }
}
</script>
<asp:CheckBox
id="chkSlow"
Text="Did you know that ASP Classic is slow?"
Runat="server" />
<br /><br />
<asp:CheckBox
id="chkOutdated"
Text="Did you know that ASP Classic is outdated?"
Runat="server" />
<br /><br />
The WebSurvey.aspx page also references the following user control.
<%@ Control Language="C#" ClassName="ASPNetSurvey" %>
<script runat="server">
public bool KnowFast
{
get { return chkFast.Checked; }
}
public bool KnowNewest
{
get { return chkNewest.Checked; }
}
</script>
<asp:CheckBox
id="chkFast"
Text="Did you know that ASP.NET is fast?"
Runat="server" />
<br /><br />
<asp:CheckBox
id="chkNewest"
Text="Did you know that ASP.NET is the newest Microsoft
technology for building Web applications?"
Runat="server" />
<br /><br />
Need ASP.NET and Visual Studio 2008 Training?
- Learn ASP.NET 3.5 from Stephen Walther, author of ASP.NET 3.5 Unleashed.
We've provided ASP.NET training for NASA, Lockheed Martin, the National Science Foundation, Verizon,
Boeing, the US House of Representatives, Kaiser, Petco, Mary Kay, and Microsoft.
Why not your company?
-
Receive a four day, hands-on, intensive workshop.
-
We fly to you, anywhere in the world.
-
We can bring our own laptops.
To learn more, visit the
Superexpert Training website.