.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