Binding an Enum to an asp:DropDownList

Binding an Enum to an asp:DropDownList

Old forum URL: forums.lhotka.net/forums/t/5115.aspx


adamnationx posted on Thursday, July 17, 2008

Hey,

I've been trying to track down a post that addresses this for a while today with no luck. I have an asp:FormView bound to my Order business object that contains a field, OrderState, which is internally tied to an Enum. I am trying to add an asp:DropDownList to the FormView which will be bound to my business object's OrderState property and populated from the Enum's values. I'm setting the Enum as the DropDownList's datasource in the code behind by iterating the enum and storing the name/value pairs in a hashtable. That seems to work fine. Here's my problem, though. When I try to bind the DropDownList's SelectedValue property to my business object's OrderState property, I get an ArgumentOutOfRangeException:


Exception Details: System.ArgumentOutOfRangeException: 'cboOrderStates' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

I'm trying to set the DropDownLists's datasource in the Page_Load handler, and walking through it in debug, I notice that the exception is thrown when I try to reference the DropDownList (before I even get to set the datasource). Here's the code I'm talking about. The line the exception is thrown on is marked.

DropDownList cboOrderStates = (DropDownList)
    FormView1.Row.FindControl("pnlPageContent").FindControl("cboOrderStates");

cboOrderStates.DataSource = BindToEnum(typeof(Order.OrderStates));  // Hashtable
cboOrderStates.DataBind();
I guess that when I try to reference the DropDownList, it binds (or realizes it has already been bound) to my BO's OrderState property. This is on an Edit page, so the BO's property loads with a non-null value, and since I haven't populated the DropDownList yet, it chokes up. Honestly, though, I could be completely wrong about that. Either way, I would really appreciate some help figuring out how to overcome this. If it helps, here's the line in my .aspx file that binds the DropDownList to my BO's OrderState property:

<asp:DropDownList ID="cboOrderStates" runat="server"
                                    DataTextField="Key" DataValueField="Value"
                                    SelectedValue='<%# Bind("OrderState") %>' />

Thanks in advance for any help you guys might be able to give me on this!!

-Adam

IanK replied on Friday, July 18, 2008

Adam,

I'm not a ASP developer but Rocky did a post last year on converting on building an namevaluelist from an Enumerator. You would then use this instead to do your binding rather than the Enumerator. Sample population of NVL where Enumerator is defined in Btier.Enumerators.States as follows:

Private Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)

Try

Me.RaiseListChangedEvents = False

IsReadOnly = False

For Each CurrentState As BTier.Enumerators.States In System.Enum.GetValues(GetType(BTier.Enumerators.States))

Add(New Csla.NameValueListBase(Of Integer, String).NameValuePair(CurrentState, _

System.Enum.GetName(GetType(BTier.Enumerators.States), CurrentState)))

Next

IsReadOnly = True

Me.RaiseListChangedEvents = True

Catch Ex As Exception

Throw Ex

End Try

End Sub

adamnationx replied on Monday, July 21, 2008

Oh, sweet. Thanks for the help, Ian!

Copyright (c) Marimer LLC