Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate DropDownList in DetailsView from database

Status
Not open for further replies.

mspain

IS-IT--Management
Mar 17, 2002
100
US
Hello all,

I am having a bit of a problem that others seem to have, but I can't find a clean answer for.

I have a DetailsView that when switched to edit mode displays a drop down list. I want this list to be populated with choices from another table. So, I have 2 object data sources. 1 for the DetailsView and 1 for the drop down list.

My problem is that when I try to set the selected value of the drop down list using a Bind expression to a column in the first table I get a

'EditOfficeDropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Error. When I remove the bind statement I see that all the correct choices are indeed there. I have tried setting AppendDataBoundFields to true on the DDL but that hasn't worked. What am I doing wrong?

Thanks!!

Code:

<asp:DetailsView ID="UserDetailsView" runat="server" AutoGenerateRows="False" DataKeyNames="UserName"
DataSourceID="UserDetailsViewDataSource" Height="50px" Width="300px" HeaderText="User Details" Visible="False" CommandRowStyle-HorizontalAlign="Left" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" GridLines="Horizontal">

<Fields>

<asp:TemplateField HeaderText="Office">
<EditItemTemplate>
<asp:DropDownList Width="150px" AppendDataBoundItems="true" ID="EditOfficeDropDownList" runat="server" DataSourceID="OfficesObjectDataSource" DataValueField="Name" DataTextField="Name" SelectedValue='<%# Bind("Office") %>'>

</asp:DropDownList>
</EditItemTemplate>

...

</asp:detailsview>

<asp:ObjectDataSource ID="UserGridViewObjectDataSource" runat="server" DeleteMethod="Delete"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetMembers" TypeName="MembershipUserODS"
UpdateMethod="Update" SortParameterName="sortData">

...

</asp:ObjectDataSource>

<asp:ObjectDataSource ID="OfficesObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetAllOffices" TypeName="OfficesBLL"></asp:ObjectDataSource
 
I had something similar but i was using a Gridview. In the code behind I had to put the function that my dropdownlist was bound to in the RowEditing procedure along with the function I used to bind the Gridview with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top