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 ❘ 1189<br />

using System;<br />

public enum Suit<br />

{<br />

Club, Diamond, Heart, Spade<br />

}<br />

The PCSUserC1 class needs a member variable to hold the suit type, currentSuit:<br />

code snippet PCSUserCWebApp1/App_Code/Suit.cs<br />

public partial class PCSUserC1 : System.Web.UI.UserControl<br />

{<br />

protected Suit currentSuit;<br />

code snippet PCSUserCWebApp1/PCSUserC1.ascx.cs<br />

And a property to access this member variable, Suit:<br />

public Suit Suit<br />

{<br />

get<br />

{<br />

return currentSuit;<br />

}<br />

set<br />

{<br />

currentSuit = value;<br />

suitPic.ImageUrl = "~/Images/" + currentSuit.ToString() + ".bmp";<br />

suitLabel.Text = currentSuit.ToString();<br />

}<br />

}<br />

The set accessor here sets the URL of the image to one of the files you copied earlier, <strong>and</strong> the text displayed<br />

to the suit name.<br />

Accessing the New Property<br />

Next, you must add code to Default.aspx so that you can access this new property. You could simply<br />

specify the suit using the property you have just added (if you compile the project the options will even<br />

appear in IntelliSense for you):<br />

<br />

code snippet PCSUserCWebApp1/Default.aspx<br />

The ASP.<strong>NET</strong> processor is intelligent enough to get the correct enumeration item from the string provided.<br />

To make things a bit more interesting <strong>and</strong> interactive, though, you will use a radio button list to select a suit:<br />

<br />

<br />

<br />

<br />

Club<br />

Diamond<br />

Heart<br />

Spade<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!