Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Programmatic Loading of User Controls


Programmatic Loading of User Controls Dino explains how to set up a user control for your web site interface so that it can be modified on the fly or based on changing runtime conditions

A user control is a kind of Web form made of any combination of server controls sewn together with managed and script code. A typical user control has a rich user interface and represents a block of reusable UI gadget. It can also optionally feature an object model and make some its encapsulated components visible to developers. All of its constituent controls are protected and inaccessible from outside components unless they're made available through a public programming interface.

A user control can be seen as an embeddable Web form that can be nested inside other container forms. You create a user control in much the same way you create a Web form. You can easily convert a page into a user control and vice versa. A user control is given a mandatory ASCX extension.

Pages see the new control as a monolithic component and treat it as a regular native server control. A user control is normally simpler to write than a custom control and doesn't usually require special ad hoc skills, as is often the case with custom controls.

Just like any other ASP.NET server control, a user control can be created via code and based on runtime conditions. Unlike server controls, though, you don't use the new operator to instantiate a user control. You rather resort to the LoadControl method defined on the page class. The method returns a Control object that it created from the contents of an ASCX file. The file refers to a virtual path.


Control ctl = thePage.LoadControl("Sample.ascx");

As long as the user control doesn't feature additional properties, you are ready to add it to the page for display. Here's the code you need:


thePage.Controls.Add(ctl);

What if the user control contains properties and methods to configure its behavior? As you can see, the LoadControl method returns a reference to a Control object; you need to cast the reference to the appropriate user control type in order to set properties and use the control to its fullest. But what's the type of the user control?

You can give a user control an explicit type name by using the ClassName attribute of the @Control directive.


<% @Control ClassName="MyUserControl" %>

However, this is still not enough. You can't just cast the object that LoadControl returns to the user control type. In order to do so, you need to have a reference to the assembly that contains the compiled code of the control. This is just the role of the @Reference directive. When you create the user control programmatically, the strong type for the user control is available to the page only after you have created a reference to it.


<%@ Reference Control="Sample.ascx" %>

The net effect of this directive is to make available in the context of the Web page a reference to a type that represents the specified control.


MyUserControl ctl;
ctl = (MyUserControl) thePage.LoadControl("Sample.ascx");
// Configure the user control
thePage.Controls.Add(ctl);

Dynamically loading user controls from an external URL is a handy trick for modifying on the fly, or based on changed runtime conditions, a piece of the page's user interface. If you need to make sure that each user control serving a given purpose has a fixed behavior, you simply inherit all user controls from a common base class or force them to implement a given interface.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.