CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Binding an Enum to an asp:DropDownList

Last post 07-21-2008, 9:53 AM by adamnationx. 2 replies.
Sort Posts: Previous Next
  •  07-17-2008, 3:11 PM 24774

    Binding an Enum to an asp:DropDownList

    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
  •  07-18-2008, 12:53 PM 24796 in reply to 24774

    Re: Binding an Enum to an asp:DropDownList

    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

  •  07-21-2008, 9:53 AM 24829 in reply to 24796

    Re: Binding an Enum to an asp:DropDownList

    Oh, sweet. Thanks for the help, Ian!
View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems