15.02.2015 Views

C# 4 and .NET 4

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

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

User <strong>and</strong> Custom Controls ❘ 1187<br />

a simple User Control<br />

In Visual Studio, create a new empty web site called PCSUserCWebApp1 in the directory C:\ProCSharp\<br />

Chapter41 . After the web site has been generated, select the Website ➪ Add New Item ... menu option <strong>and</strong><br />

add a Web User Control called PCSUserC1.ascx .<br />

The fi les added to your project, with the extensions .ascx <strong>and</strong> .ascx.cs , work in a very similar way to the<br />

.aspx fi les that you have seen already. The .ascx fi le contains your ASP.<strong>NET</strong> code <strong>and</strong> looks very similar<br />

to a normal .aspx fi le. The .ascx.cs fi le is your code - behind fi le, which defi nes custom code for the user<br />

control, much in the same way that forms are extended by .aspx.cs fi les.<br />

The .ascx fi les can be viewed in Design or Source view, just like .aspx fi les. Looking at the fi le in Source<br />

view reveals an important difference: there is no HTML code present, <strong>and</strong> in particular no element.<br />

This is because user controls are inserted inside ASP.<strong>NET</strong> forms in other fi les <strong>and</strong> so don ’ t need a <br />

tag of their own. The generated code is as follows:<br />

< %@ Control Language="<strong>C#</strong>" AutoEventWireup="true" CodeFile="PCSUserC1.ascx.cs"<br />

Inherits="PCSUserC1" % ><br />

code snippet PCSUserCWebApp1/PCSUserC1.ascx<br />

This is very similar to the < %@ Page % > directive generated in .aspx fi les, except that Control is specifi ed<br />

rather than Page . The CodeFile attribute specifi es the code - behind fi le <strong>and</strong> Inherits specifi es the class<br />

defi ned in the code - behind fi le from which the page inherits. The code in the .ascx.cs fi le contains, as<br />

in auto - generated .aspx.cs fi les, a class defi nition that is empty apart from a Page_Load() event<br />

h<strong>and</strong>ler method.<br />

Your simple control will be one that displays a graphic corresponding to one of the four st<strong>and</strong>ard suits<br />

in cards (club, diamond, heart, or spade). The graphics required for this were shipped as part of a<br />

previous version of Visual Studio; you can fi nd them in the downloadable code for this chapter, in the<br />

CardSuitImages directory, with the fi lenames CLUB.BMP , DIAMOND.BMP , HEART.BMP , <strong>and</strong> SPADE.BMP . Copy<br />

these fi les into a new Images subdirectory of your project ’ s directory, so that you can use them in a moment.<br />

If you do not have access to this download, you can use any images you like for this example because they<br />

are not important to the functionality of the code.<br />

Note that unlike earlier versions of Visual Studio, changes you make to the web site<br />

structure outside of Visual Studio are automatically refl ected in the IDE. You have to<br />

hit the refresh button in the Solution Explorer window, but you should see the new<br />

Images directory <strong>and</strong> bitmap fi les appear automatically.<br />

Adding Content to the Control<br />

Now add some code to your new control. In the HTML view of PCSUserC1.ascx , add the following:<br />

<br />

<br />

<br />

<br />

<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!