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:

public partial class NewsHeadline : System.Web.UI.UserControl

{

 

}

 

public enum NewsCategory

{

    BreakingNews=0,

    Business=1,

    Sports=2,

    Money=3,

    Local=4

}

My public enumeration property

using System.ComponentModel;

 

public partial class NewsHeadline : System.Web.UI.UserControl

{

    public NewsCategory _category { get; set; }

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

    public NewsCategory 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.

    private void DisplayNewsByEnum(NewsCategory value)

    {

        switch (View)

        {

            case NewsCategory.BreakingNews:

                Label1.Text = “Breaking News”;

                break;

            case NewsCategory.Business:

                Label1.Text = “Business”;

                break;

            case NewsCategory.Sports:

                Label1.Text = “Sports”;

                break;

            case NewsCategory.Money:

                Label1.Text = “Money”;

                break;

            case NewsCategory.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

More