views:

335

answers:

2

(first of all, it seems this is a subject that is discussed many times before, but I can't find a proper answer for my case)

I have a asp.net FormView with a DropDownList for the selection of a month. The FormView is data bound to an ObjectDataSource.

<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Bind("OrderDate.Month") %>' Width="100" runat="server" />

I like to bind the selected value to the nested property 'Month' of 'OrderDate' as shown above. The property OrderDate is of type DateTime. The error I'm getting while binding to a nested property is:

A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind.

What is the best solution to be able to bind to a nested propety?

Thanks in advance.

+1  A: 

What about using Eval keyword

<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Eval("OrderDate.Month") %>' Width="100" runat="server" />
nandokakimoto
hm ... this is not safe, as `SelectedValue` might throw an exception. you should rather go for the `ItemDataBound`-event!
Andreas Niedermair
That will do, but I'd like to use Bind(), because the selected value must be returned to the datasource when the form is submitted.
Monty
why do you want to use `Bind()`. do you need a two-way-binding?... on an immutable-type??
Andreas Niedermair
Nice one Andreas, looked over it. Indeed I need two-way binding, but this way it's not gonna happen for this specific type. Have to find another way. Thanks.
Monty
+1  A: 

Are you retrieving data from DataSourceID="MonthsListDataSource" and tryingo to bind it to another DataBase field (SelectedValue='<%# Eval("OrderDate.Month") %>') ?

In my application a do this like this:

<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Eval("MonthsListDataSource.Month") %>' Width="100" runat="server" />

And when Updating a retrieve de DropDownList with find control, get its selected value, associate it to other table (OrderDate.Month) and save it.

Sorry my answer, but i dont know if a undertand it.

Ewerton
I like your solution, very clean one. Thanks.
Monty
i'am happy to help
Ewerton