ARTICLE

Using Reference Directive in ASP.Net

Posted by Abhimanyu Kumar Vatsa Articles | ASP.NET using VB.NET August 23, 2010
In this article we will discuss how to use Reference Directive.
Reader Level:

Introduction

In general when we load a User control with the Page.LoadControl() method, the User control is returned as an instance of the System.Web.UI.Control class. This means that if the User control includes any custom properties, the properties aren't available when we dynamically load the User control. If we dynamically load a User control, then we need to cast the control to the correct type before we can access any of the control's custom properties. To get a reference to a User control's type, we must use the <%@ Reference %> directive.

Taking an example, imagine that we need to create a form that displays different questions depending on the answers that a user provides for previous questions. In that case, we can dynamically load different User controls that contain the different sets of questions. The page given below contains a survey form. The first question asks us whether we are currently using VS 4.0 or VS 3.5 and depending on our answer, the remainder of the form displays different questions.

Default2.aspx File Code

<%@ Page Language="VB" %>
<%@ 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 _survey As Control = Nothing

    Private Sub Page_Load()
        Select Case ddlLanguage.SelectedIndex
            Case 1
                _survey = Page.LoadControl("ASPSurvey.ascx")
            Case 2
                _survey = Page.LoadControl("ASPNetSurvey.ascx")
        End
Select

        If Not IsNothing(_survey) Then
            PlaceHolder1.Controls.Add(_survey)
        End If
    End
Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
        Select Case ddlLanguage.SelectedIndex
            Case 1
                Dim aspResults As ASPSurvey = CType(_survey, ASPSurvey)
                ltlResults.Text = "<h1>ASP Survey</h1>"
                ltlResults.Text += "<br/>ASP is fast? " + aspResults.KnowFast.ToString()
                ltlResults.Text += "<br />ASP is very difficult to learn? " + aspResults.KnowNewest.ToString()
            Case 2
                Dim aspNetResults As ASPNetSurvey = CType(_survey, ASPNetSurvey)
                ltlResults.Text = "<h1>PHP Survey</h1>"
                ltlResults.Text += "<br />PHP is fast? " + aspNetResults.KnowSlow.ToString()
                ltlResults.Text += "<br />PHP is very difficult to learn? " + aspNetResults.KnowOutdated.ToString()
        End Select
    End
Sub

</script>
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <style type="text/css">
        html
        {
            font:14px Arial,Sans-Serif;
            background-color:Aqua;
        }
    </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"  />
        <asp:ListItem Text="PHP" />
    </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>

Now depending on the user's selection from the DropDownList control, one of two User controls is loaded in the Page_Load() event handler: the ASPSurvey.ascx or the PHPSurvey.ascx User control. When we submit the survey form, the btnSubmit_Click() method executes. This method casts the User control loaded in the form to the correct type. It casts the User control to either the ASPSurvey or the PHPSurvey type.

ASP.Survey.ascx File Code

<%@ Control Language="VB" ClassName="ASPSurvey" %>

<script runat="server">
 
    Public ReadOnly Property KnowFast() As Boolean
        Get
            Return chkFast.Checked
        End Get
    End Property
 
    Public ReadOnly Property KnowNewest() As Boolean
        Get
            Return chkNewest.Checked
        End Get
    End
Property
</script>
<
asp:CheckBox
    id="chkFast"
    Text="Did you know that ASP is fast?"
    Runat="server"
/>

<br /><br />

<asp:CheckBox
    id="chkNewest"
    Text="Did you know that ASP is very difficult to learn?"
    Runat="server"
/>
<br /><br /><br />

PHPSurvey.ascx File Code

<%@ Control Language="VB" ClassName="PHPSurvey" %>

<script runat="server">
 
    Public ReadOnly Property KnowSlow() As Boolean
        Get
            Return chkSlow.Checked
        End Get
    End
Property

    Public ReadOnly Property KnowOutdated() As Boolean
        Get
            Return chkOutdated.Checked
        End Get
    End
Property
</script>

<asp:CheckBox
    id="chkSlow"
    Text="Did you know that PHP is slow?"
    Runat="server"
/>

<br /><br />

<asp:CheckBox
    id="chkOutdated"
    Text="Did you know that PHP is very difficult to learn?"
    Runat="server"
/>
<br /><br /><br />

image1.gif

It is very easy to develop and widely used over web.

HAVE A GREAT CODING!
 

Login to add your contents and source code to this article
share this article :
post comment
 

reference control is very simple..try reference .aspx

Posted by Marina Tekik Sep 12, 2011
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor