Home > 2010 Winter BTI420 > “User controls” that enable modularity and reuse

“User controls” that enable modularity and reuse

January 25, 2010 Leave a comment Go to comments

You will soon need to know how to create a modular component that can be coded once, and then easily placed on any web form. In ASP.NET, a "user control" is such a component.


Introduction to "user controls"

A user control is a modular component that can be placed on a web form. The user control consists of markup (ASPX) and code (C#).

Although it sounds similar to a web form, a user control cannot be executed on its own – it must be hosted on a web form.

A user control’s markup can include any XHTML element other than the html, head, body, and form "container" elements. A user control’s code is just another partial class.


Accessing user control members from the host page

If you want to reference the members of the user control, the recommended way is to create public fields or properties in the user control. Follow these steps:

Create suitable properties in the user control’s code file. For example:

    // Read-only property declaration
    // Enables it to be accessed from a host page
    public string SomeData
    { get { return "Hello, World!"; } }

On your web form, after you declare (drag-drop) a user control, notice its ID:

In design view, it is the value following the pound (#) sign, in this case, mySimpleUC1:

User control in design view

In source code view, it is found in the markup’s declaration:

    <uc1:mysimpleuc id="mySimpleUC1" runat="server" />

In your web form’s code, use the ID to refer to its members:

    lblResult.Text = mySimpleUC1.SomeData;


Accessing host page members from the user control

A user control is intended to be a modular component that can be placed on ANY web form. In addition, a user control shouldn’t really need or want to know anything about its host page.

Therefore, you really should not attempt to access host page members from a user control.

There are few exceptions to this guidance, but one of the exceptions is where you want to access a host page member that you know exists for sure. For example, the Page.Request.Url property (ToString() method) will get you the URL of the host page. Today’s user control example illustrates these ideas.


Referencing external resources (e.g. images) from a user control

Please note the following information from the ASP.NET User Controls Overview page:

Referencing External Resources
(web site paths)

When a user control runs, references to external resources such as images or anchors to other pages are resolved using the URL of the user control as the base URL.

For example, if the user control contains an Image control whose ImageUrl property is set to Images/Button1.gif, the URL of the image is added to the URL of the user control to resolve the complete path to the image.

If the user control references a resource that is not in a subfolder of the user control itself, you must provide a path that resolves to the correct folder at run time.


Categories: 2010 Winter BTI420
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: