Browse DevX
Sign up for e-mail newsletters from DevX


ASP.NET: Extending the Power of Web Pages with User Controls : Page 4

User controls represent an interesting and powerful level of page reusability in ASP.NET. You can build user controls by extracting and combining blocks of code from existing pages. You can also assemble ASP.NET controls together in a sort of embeddable form for use in Web pages. In this article, you'll get a crash-course on how to design, write and use ASP.NET user controls.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

All ASP.NET controls must be linked to the host page. The controls in the .NET Framework belong to the <asp> system-provided namespace. Custom controls, including user controls, need to have a prefix and a tag name so that the ASP.NET parser can correctly map tags with a living instance of the control.

You register a user control for use with an ASP.NET page by using the @Register directive:

<%@ Register TagPrefix="expo" TagName="DateBox" Src="datebox.ascx" %>

The TagPrefix and TagName attributes can take arbitrary text that you then use to insert an instance of the control in the page. The Src attribute indicates the URL from which the source of the control must be downloaded. Listing 3 shows a sample ASP.NET page that uses the DateBox control.

The default date of the control can be set in two ways: either using the SelectedDate property or the SetDate method. The property can also be set declaratively in the control's declaration. The method, instead, provides a great deal of flexibility since you can use it with code.

<expo:datebox runat="server" id="theDateBox" separator="-" selecteddate="7/1/2002" />

In Listing 3, the default date is set programmatically in the Page_Load event. When you click the link button, the control retrieves the current date and displays it through the page. Notice that the control does not cache the current date in internal variables. It just reads the current date directly from the textbox controls whenever needed.

Strongly-Typed References
As long as you write standalone ASP.NET pages, in which all the code is resident in the <script> block, you normally don't need to strictly reference the user control in a strongly typed manner. Strong-typing, though, provides you with cool facilities like IntelliSense support. To create a strong-typed reference, you must also add the @Reference directive to the page.

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

Notice that @Reference does not replace @Register, so use both if you want strong typing. The @Reference directive tells the ASP.NET runtime that a user control source file should be dynamically linked against the page. At this point, you can cast the reference to the control to a variable of the correct type (see the sidebar "IDs and Control Containers").

DateBox d = (DateBox) theDateBox; if (!IsPostBack) d.SetDate("2/2/2002");

A little problem may arise if you want to use a strong-typed reference to a user control in the context of a code-behind ASP.NET page. In this case, how can you link the user control class to a C# or Visual Basic code-behind class? You need a trick like the @Reference directive. However, the directive works by forcing the runtime to create an assembly which is then linked to the application represented by the page. The trick is simple: write the user control in code-behind mode, compile the control's code-behind class and then reference the assembly.

User controls are good at prototyping new controls that have a complex user interface. They are the perfect solution when you need to combine two or more controls. As long as the structure of the new controls is stable during the lifetime of the program, user controls offer you an excellent mix of programming ease and productivity. The architectural principle behind a user control is aggregation rather than inheritance. If you need only an enhanced or stripped-down version of an existing control, you are better off writing a new class that inherits from it.

Further Readings
Dino Esposito, Building Web Solutions with ASP.NET and ADO.NET, Microsoft Press, 2002

Dino Esposito is Wintellect's ADO.NET expert and a trainer and consultant based in Rome. He is the author of Building Web Solutions with ASP.NET and ADO.NET and the upcoming Applied XML Programming for Microsoft .NET, both from Microsoft Press. Reach him at dinoe@wintellect.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date