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