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.

298 x CHAPTER 8 USER CONTROLS<br />

The cool th<strong>in</strong>g about enums is that you will get IntelliSense <strong>in</strong> code files, <strong>in</strong> the Properties Grid,<br />

<strong>and</strong> even <strong>in</strong> the code editor for your user controls. Figure 8-6 shows how IntelliSense kicks <strong>in</strong> for a<br />

<strong>VB</strong>.<strong>NET</strong> code file.<br />

FIGURE 8-6<br />

In Figure 8-7 you see the same list with values from the enum <strong>in</strong> the Properties Grid.<br />

FIGURE 8-7<br />

And <strong>in</strong> Figure 8-8 you see the same list appear for a property of a user control <strong>in</strong> Markup View.<br />

FIGURE 8-8<br />

Just as with other code files like classes, you should put your enums <strong>in</strong> a file under the App_Code<br />

folder. If you have more than one of them, you can store them all <strong>in</strong> the same file or create a separate<br />

file for each enum.<br />

Enums are great for simple <strong>and</strong> short lists. They help you f<strong>in</strong>d the right item quickly without memoriz<strong>in</strong>g<br />

“magic numbers” like 0 or 1, but enable you to use human-readable text str<strong>in</strong>gs <strong>in</strong>stead.<br />

In the next exercise, you see how to create an enum <strong>and</strong> use it <strong>in</strong> your Banner user control.<br />

TRY IT OUT<br />

Creat<strong>in</strong>g Smarter User Controls<br />

In this exercise, you add a second banner to the user control. This banner displays as a horizontal<br />

image <strong>in</strong>side its own panel. To avoid the two banners show<strong>in</strong>g up at the same time, you add a property<br />

that determ<strong>in</strong>es which banner to display. Pages that use the control can then def<strong>in</strong>e the correct banner.<br />

1. Start by creat<strong>in</strong>g an enumeration that conta<strong>in</strong>s two members for the different directions: vertical<br />

<strong>and</strong> horizontal. To do this, right-click the App_Code folder <strong>and</strong> choose Add Í Add New Item.<br />

Choose your programm<strong>in</strong>g language <strong>and</strong> add a class file called Direction.

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

Saved successfully!

Ooh no, something went wrong!