Related Downloads

Download Resource EnumObject Class

Download the EnumObject class used in this example.
Added on 10 February 2009 10:29:21

How would you rate this article?

Rating: 0 user(s) have rated this article
Posted by: retro
Date: 10/02/2009
Category: Web Development
Views: this article has been read 1427 times

Recent Articles Get the RSS feed

(26/06/2010)

Nochex merchant accounts provide you with everything you need to accept payments on your web site. With no monthly fees and support for a number of ecommerce solutions, including nopCommerce, it has never been easier to start selling online!

(25/05/2010)

Check out our latest project for community interest company S.C.A.

(25/05/2010)

We are pleased to announce support for version 4.0 of the .NET Framework on all of our hosting plans.

(11/02/2010)

We have just completed development of a new web site for UK based Aerial Spares.

(11/02/2010)

Today sees the release of the official nopCommerce user guide. It explains every part of the application in detail and includes a getting started guide so you can get up and running quickly.

read more read more

A while ago we posted an article that explained how to create a custom class to wrap enumerations so that their members can be accessed using a standard ObjectDataSource control and binded to a control such as a DropDownList. This relied on creating a separate method for each enumeration. Here we have just one method for any enumeration.

The original article can be viewed at http://www.retroviz.com/CMS/a79_Creating_custom_data_objects_using_enumerations.aspx

Instead of creating a method for each enumeration that you wish to retrieve, we can create a generic method and pass it the name of the type. The updated EnumObject class can be seen below:

    <DataObjectAttribute()> _
    Public Class EnumObject
        Private _value As Integer
        Private _name As String
        Public Property Value() As Integer
            Get
                Return _value
            End Get
            Set(ByVal value As Integer)
                _value = value
            End Set
        End Property
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
        Public Sub New()
        End Sub
        Public Sub New(ByVal value As Integer, ByVal name As String)
            Me.Value = value
            Me.Name = name
        End Sub
        ''' <summary>
        ''' Returns the items in an enumeration ignoring "notset"/0
        ''' </summary>
        ''' <param name="enumName">The full name (including namespace) of the enumeration</param>
        <DataObjectMethod(DataObjectMethodType.Select, False)> _
        Public Shared Function GetEnumTypes(ByVal enumName As String) As List(Of EnumObject)
            Dim recordset As New List(Of EnumObject)
            Dim s As Type = Type.GetType(enumName)
            Dim fi As FieldInfo() = s.GetFields
            For Each f As FieldInfo In fi
                If Not f.IsSpecialName AndAlso Not (f.Name.ToLower = "notset") Then
                    Dim myValue As Integer = CType(f.GetValue(0), Integer)
                    recordset.Add(New EnumObject(myValue, f.Name))
                End If
            Next
            Return recordset
        End Function
    End Class

Then we create our ObjectDataSource we just pass the full name of our type (including any namespaces):

    <asp:ObjectDataSource runat="server" ID="objMaintenanceType" SelectMethod="GetEnumTypes"
        TypeName="MyApp.BLL.EnumObject">
        <SelectParameters>
            <asp:Parameter DefaultValue="MyApp.BLL.MaintenanceType" Name="enumName"
                Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>

and bind a data control to it like below:

 <asp:DropDownList runat="server" ID="ddlMaintenanceType" AutoPostBack="True" DataSourceID="objMaintenanceType"
                    DataTextField="Name" DataValueField="Value">

It's as simple as that.

Send to Friend  Send to friend

Comments

Comment this article
Name:
E-mail:
Comment:
Add Cancel