.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET: Create a User Control Property with Selectable Options


I guess I’ve always taken those little options for granted whenever I want to pick something in a control, say what mode to put a textbox in (text, multiline, or password), because when I wanted to add something similar for a user control, I had no idea how to do it. Thanks to the friendly and extremely helpful people at StackOverflow, I was able to grok the answer.

Intellisense options

Here’s the scenario: you have a user control in your .aspx page with one property, say “addedorapproved” as in the above image. You want the options “Added” and “Approved” to show up as your selectable options. To do so, you must complete the following steps:

  1. Create an enum with your available options:
    Enum AddApproveOptions
    Added
    Approved
    End Enum
  2. Create your private member as your Enum:
    Private _addedapproved As AddApproveOptions
  3. Create your property as your Enum:
    Public Property AddedOrApproved() As AddApproveOptions
    Get
    Return _addedapproved
    End Get
    Set(ByVal value As AddApproveOptions)
    _addedapproved = value
    End Set
    End Property

 And that’s it! You should see the little Intellisense box pop up with your options for your new property!

Advertisements

February 5, 2009 - Posted by | ASP.NET, Tips & Tricks, VB.NET, Visual Studio.NET

4 Comments »

  1. Thank you so much for this tutorial. I’ve been looking for this since forever. Works great!!

    Comment by Tony | February 18, 2009 | Reply

  2. Glad I could help!

    Comment by Some.Net(Guy) | February 23, 2009 | Reply

  3. Ну ты даёшь!

    Comment by Джульета | May 22, 2009 | Reply

  4. C# version

    In your user control –

    public enum OrientationOption
    {
    Horizontal,
    Vertical
    }

    public OrientationOption Orientation { get; set; }

    Comment by Alex Z | February 23, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: