03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

8User<br />

Controls<br />

WHAT YOU WILL LEARN IN THIS CHAPTER:<br />

‰ What user controls are, how they look, <strong>and</strong> why they are useful<br />

‰ How to create user controls<br />

‰ How to consume (or use) user controls <strong>in</strong> your pages<br />

‰ How you can improve the usefulness of user controls by add<strong>in</strong>g<br />

cod<strong>in</strong>g logic to them<br />

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER<br />

You can f<strong>in</strong>d the wrox.com code downloads for this chapter on the Download Code tab at<br />

www.wrox.com/remtitle.cgi?isbn=1118311809. The code is <strong>in</strong> the Chapter 8 download.<br />

In addition to the master pages, themes, <strong>and</strong> sk<strong>in</strong>s discussed <strong>in</strong> Chapter 6, <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> has<br />

another feature that enables you to create reusable <strong>and</strong> thus consistent blocks of <strong>in</strong>formation:<br />

user controls.<br />

User controls enable you to group logically related content <strong>and</strong> controls together so they can<br />

be treated as a s<strong>in</strong>gle unit <strong>in</strong> content pages, master pages, <strong>and</strong> <strong>in</strong>side other user controls. A<br />

user control is actually a sort of m<strong>in</strong>i-<strong>ASP</strong>X page <strong>in</strong> that it has a markup section <strong>and</strong>, optionally,<br />

a Code Beh<strong>in</strong>d file <strong>in</strong> which you can write code for the control. Work<strong>in</strong>g with a user control<br />

is very similar to work<strong>in</strong>g with normal <strong>ASP</strong>X pages, with a few m<strong>in</strong>or differences.<br />

In versions of <strong>ASP</strong>.<strong>NET</strong> before 2.0, user controls were often used to create blocks of reusable<br />

functionality that had to appear on every page <strong>in</strong> the site. For example, to create a menu,<br />

you would create a user control <strong>and</strong> then add that control to each <strong>and</strong> every page <strong>in</strong> the site.<br />

Because of the <strong>ASP</strong>.<strong>NET</strong> support for master pages, you don’t need user controls for these<br />

scenarios anymore. This makes it easier to make changes to your site’s structure. Despite the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!