Using Enumerated Properties on User Controls

In this example we’re going to create a user control with an enumerated property that you will be able to choose via Intellisense.

I’m going to begin by creating my user control NewsHeadline.ascx:

clip_image002

Then in my code behind I’ll create an enumeration that contains all of the news categories I’ll be dealing with:

publicpartialclassNewsHeadline : System.Web.UI.UserControl

{

}

publicenumNewsCategory

{

BreakingNews=0,

Business=1,

Sports=2,

Money=3,

Local=4

}

My public enumeration property
using System.ComponentModel;

publicpartialclassNewsHeadline : System.Web.UI.UserControl

{

publicNewsCategory _category { get; set; }

[Browsable(true)]   // Needed to display in Intellisense popup

publicNewsCategory View

{

get

{

return _category;

}

set

{

_category = value;

DisplayNewsByEnum(_category);

}

}

}

I’m then passing in the selected NewsCategory into another method:

I choose to use a switch statement since Visual Studio will auto-generate your cases once you enclose the enumeration in the parenthesis.


    privatevoid DisplayNewsByEnum(NewsCategory value)

{

switch (View)

{

caseNewsCategory.BreakingNews:

Label1.Text = “Breaking News”;

break;

caseNewsCategory.Business:

Label1.Text = “Business”;

break;

caseNewsCategory.Sports:

Label1.Text = “Sports”;

break;

caseNewsCategory.Money:

Label1.Text = “Money”;

break;

caseNewsCategory.Local:

Label1.Text = “Local”;

break;

default:

Label1.Text = “No selection made”;

break;

}

}

****

That’s all you need, you may need to build the site once before the enumeration shows up but be patient and it should recognize it no problem.

image

As well as in design mode

image

Show Comments