Re-Binding A DropDownList Within A DetailsView

4

47computers

Hello. I'm having a bit of difficulty with some DropDownList controls
within templates in a DetailsView on my web form. Basically, I have a
form where a user is building a "location profile" with a handful of
fields. Two of the controls are a DropDownList of countries and a
DropDownList of states (populated from a provider that is known to be
working elsewhere in the website).

The idea is simple... If a user selects a different country than what
is currently selected, that control auto-posts and repopulates the
states with the correct list for that country. This works fine on a
form that's manually created, but is not working yet in a
DetailsView. Here is the code for these two rows and their templates
(sorry for any wrapping):


<asp:TemplateField HeaderText="Country" SortExpression="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%#
Country.GetCountryByID(Convert.ToInt32(Eval("CountryID"))).Name %>'></
asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:ObjectDataSource ID="objCountries" runat="server"
TypeName="Country" SelectMethod="GetCountries" />
<asp:DropDownList ID="ddlCountry" runat="server" Width="99%"
SelectedValue='<%# Bind("CountryID") %>' DataSourceID="objCountries"
DataTextField="Name" DataValueField="ID" AutoPostBack="true" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State" SortExpression="State">
<ItemTemplate>
<asp:Label ID="lblState" runat="server" Text='<%#
State.GetStateByID(Convert.ToInt32(Eval("StateID"))).Name %>'></
asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:ObjectDataSource ID="objStates" runat="server"
TypeName="State" SelectMethod="GetStates">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCountry"
PropertyName="SelectedValue" Name="countryID" Type="Int32"
DefaultValue="225" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="ddlState" runat="server" Width="99%"
SelectedValue='<%# Bind("StateID") %>' DataSourceID="objStates"
DataTextField="Name" DataValueField="ID" />
</EditItemTemplate>
</asp:TemplateField>


This works fine when the page initially loads. The list of countries
is sorted alphabetically, so Afganistan is first. And the list of
states gets populated with the correct provinces based on the data
source. (I'll worry about conveniently defaulting to the US for the
majority of users later.) However, when I select a different country,
the following exception is thrown:


"Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control."


So far my testing has only involved using the DetailsView to insert a
record, so I'm only using the EditItemTempates as of yet. Can anyone
offer any advice on why this might be happening?


Regards,
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top