Control Pages’ Contents

Give your user controls flexibility with User Control Properties.

A Day in the Life of a Developer

LANGUAGES: VB .NET

TECHNOLOGIES: User Control Properties

 

Control Pages' Contents

Give your user controls flexibility with User Control Properties.

 

By Doug Seven

 

Ah, a new week, a new task. This week I needed to build a user control whose content would be rendered differently on different pages. Specifically, this user control contains a TextBox for submitting an e-mail address to register for a newsletter. In some cases, it's important to show a legal disclaimer, while in others it's not. Rather than separating the two items into separate controls, I created a single user control that exposes a property to control this behavior.

 

The user control has two Panel controls, each populated with content that can be identified as a single item:

 

<asp:Panel id="Panel1" runat="server">

<HR width="100%" SIZE="1">

This is a boring corporate legal disclaimer.

It will be shown only on some

pages.

<HR width="100%" SIZE="1">

 </asp:Panel>

<asp:Panel id="Panel2" runat="server">

  <P><STRONG>Register Now:</STRONG><BR>

    E-Mail:

    <asp:TextBox id="TextBox1"

  runat="server"></asp:TextBox>

    <asp:LinkButton id="LinkButton1"

  runat="server">Go!</asp:LinkButton></P>

</asp:Panel>

 

The first Panel is the legal disclaimer; the second contains the registration controls.

 

Exposing a property in a user control is similar to exposing a property in any class. In the code-behind class for the user control, you create a private variable as the holder of the property value. Then create a property object with set and get accessors:

 

        Private _showDisclaimer As Boolean = True

        Public Property ShowDisclaimer() As Boolean

            Get

                Return _showDisclaimer

            End Get

            Set(ByVal Value As Boolean)

                 _showDisclaimer = Value

            End Set

        End Property

 

In the Page_Load event handler, simply set the value of the Panel control's Visible property to hide or show the panel:

 

Private Sub Page_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.Load

        Panel1.Visible = _showDisclaimer

End Sub

 

Now you can set this property's value from any Web form that hosts the user control. By default, I set up this property to show the disclaimer. So if you need the disclaimer, you don't have to do anything; if you want to hide the disclaimer, you set the MyUserControl.ShowDisclaimer property to false:

 

Private Sub Page_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.Load

         MyUserControl1.ShowDisclaimer = False

End Sub

 

Because the ShowDisclaimer property in a user control works the same as it does in any other class, you can set its value even when loading the user control dynamically. When you load a user control dynamically, you use the LoadControl method, which returns the user control as its base type, System.Web.UI.Control. Because the System.Web.UI.Control class does not expose the ShowDisclaimer property, you must first cast the object as its correct type. Once the object is cast correctly, you can set its properties and add it to the Controls collection of the server-side form (the second control in the Web form's controls collection):

 

Private Sub Page_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.Load

        Dim c As Control = LoadControl("UC/MyUserControl.ascx")

        CType(c, UC.MyUserControl).ShowDisclaimer = False

        Me.Controls(1).Controls.Add(c)

End Sub

 

The code for the date picker is available for download.

 

As a co-founder of DotNetJunkies (http://www.dotnetjunkies.com), a content-based online training resource for .NET developers, Doug Seven has been building applications with the .NET Framework since summer 2000. Seven has co-authored five books related to the .NET Framework, including Programming Data-Driven Web Applications with ASP.NET and ASP.NET: Tips, Tutorials & Code (Sams), and he has worked with C#, Visual Basic .NET, Web applications, mobile device applications, XML Web Services, Windows Forms development, and console and service applications. E-mail Doug at mailto:[email protected].

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish