8.4. Summary

User controls can greatly improve the maintainability of your site. Instead of repeating the same markup and code on many different pages in your site, you encapsulate the code in a single control, which can then be used in different areas of your site.

To incorporate a user control in a web page, you need to register it first. You can do this on a page-by-page basis using an @ Register directive at the top of the page that defines the tagname, the tagprefix, and the source of the user control. Alternatively, controls that are going to be used a lot can be registered in the web.config file in the <controls> element.

To improve the usefulness of your controls, you can add behavior to them. It's common to create controls with properties you can set in consuming pages allowing you to change the behavior of the control at runtime. You saw how to create a property that uses a custom enumeration to determine the type of banner to display. You also learned how to create an advanced ViewState property that is able to maintain its state across postbacks. With these kinds of properties it becomes much easier to programmatically change the behavior of the control.

Although ViewState properties can solve some of the state issues you may come across, you should carefully consider whether you really need them or not. Since these properties add to the size of the page, they can have a negative impact on your site's performance.

You can improve the Banner control by keeping track ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.