Chapter 8. User Controls

WHAT YOU WILL LEARN IN THIS CHAPTER:

  • What user controls are, how they look, and why they are useful

  • How to create user controls

  • How to consume (or use) user controls in your pages

  • How you can improve the usefulness of user controls by adding coding logic to them

Besides master pages, themes, and skins discussed in Chapter 6, ASP.NET 4 has another feature that enables you to create reusable and thus consistent blocks of information: user controls.

User controls enable you to group logically related content and controls together so they can be used as a single unit in content pages, master pages, and inside other user controls. A user control is actually a sort of mini-ASPX page in that it has a markup section and optionally a Code Behind file in which you can write code for the control. Working with a user control is very similar to working with normal ASPX pages with a few minor differences.

In versions of ASP.NET before 2.0, user controls were often used to create blocks of reusable functionality that had to appear on every page in the site. For example, to create a menu, you would create a user control and then add that control to each and every page in the site. Due to the ASP.NET support for master pages, you don't need user controls for these scenarios anymore. This makes it easier to make changes to your site's structure. Despite the advantages that master pages bring, there is still room for user controls in your ASP.NET web sites, as you discover in this ...

Get Beginning ASP.NET 4: 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.