03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

290 x CHAPTER 8 USER CONTROLS<br />

How It Works<br />

The design experience of user controls <strong>in</strong> the Visual Studio IDE is identical to that of pages. You can<br />

use drag <strong>and</strong> drop; the Toolbox; the Markup, Split, Design Views; <strong>and</strong> so on. This makes it easy to<br />

work with user controls because you can use all the familiar tools you also use for page development.<br />

The control you just created displays a s<strong>in</strong>gle image wrapped <strong>in</strong> an anchor element. In the next section,<br />

you see how to add the user control to the master page so it will be displayed <strong>in</strong> the Sidebar<br />

element of every page <strong>in</strong> the site. Later sections <strong>in</strong> this chapter show you how to add the<br />

other image that you can use to display a horizontal banner <strong>in</strong> <strong>in</strong>dividual content pages.<br />

Add<strong>in</strong>g User Controls to a Content Page or Master Page<br />

To use a user control <strong>in</strong> a content or master page or <strong>in</strong> another user control, you need to perform<br />

two steps. First, you need to register the control by add<strong>in</strong>g an @ Register directive to the page or<br />

control where you want the user control to appear. The second step <strong>in</strong>volves add<strong>in</strong>g the tags for the<br />

user control to the page <strong>and</strong> optionally sett<strong>in</strong>g some attributes on it.<br />

A typical @ Register directive for a user control looks like this:<br />

<br />

The directive conta<strong>in</strong>s three important attributes, described <strong>in</strong> the follow<strong>in</strong>g table.<br />

Attribute<br />

Src<br />

TagName<br />

TagPrefix<br />

Description<br />

Po<strong>in</strong>ts to the user control you want to use. To make it easier to move pages at a<br />

later stage, you can also use the tilde (~) syntax to po<strong>in</strong>t to the control from the<br />

application root.<br />

The name for the tag that is used <strong>in</strong> the control declaration <strong>in</strong> the page. You’re free<br />

to make up this name, but usually it is the same as the name of the control.<br />

Holds the prefix of the TagName that is used <strong>in</strong> the control declaration. Just as<br />

<strong>ASP</strong>.<strong>NET</strong> uses the asp prefix to refer to its controls, you need to provide a prefix for<br />

your own user controls. By default, this prefix is uc followed by a sequential number,<br />

but you can also change it to your own lik<strong>in</strong>g—for example, to your own company<br />

name or a custom abbreviation.<br />

Consider<strong>in</strong>g the user control you created <strong>in</strong> the preced<strong>in</strong>g exercise, your @ Register directive could<br />

look like this:<br />

<br />

When the control is registered, you can add it to the page us<strong>in</strong>g the TagPrefix:TagName construct,<br />

similar to the way you add st<strong>and</strong>ard server controls to a page. Given the @ Register directive for<br />

the banner control, you need the follow<strong>in</strong>g markup to add the control to your page:<br />

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

Saved successfully!

Ooh no, something went wrong!