Re-Binding A Dropdownlist In A DetailsView
I'm implementing many of the lessons from this book in a website project I've been working on, and one of the things that's giving me a bit of trouble is a little extra twist on the DetailsView for the addeditarticle.aspx page.
Instead of having text fields for the location information for Country and State, I have Dropdownlists populated from another provider I wrote for getting data from a source of reference information. The list of states should re-bind when a different country is selected.
The code snippet for these rows in the DetailsView is as follows (sorry for any wrapping):
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<asp:Label ID="lblCountry" runat="server" Text='<%# Country.GetCountryByID(Convert.ToInt32(Eval("Count ryID"))).Name %>'></asp:Label>
<asp:ObjectDataSource ID="objCountries" runat="server" TypeName="Country" SelectMethod="GetCountries" />
<asp:DropDownList ID="dropDownListCountry" runat="server" Width="99%" SelectedValue='<%# Bind("CountryID") %>' DataSourceID="objCountries" DataTextField="Name" DataValueField="ID" AutoPostBack="true" />
<asp:TemplateField HeaderText="State" SortExpression="State">
<asp:Label ID="lblState" runat="server" Text='<%# State.GetStateByID(Convert.ToInt32(Eval("StateID") )).Name %>'></asp:Label>
<asp:ObjectDataSource ID="objStates" runat="server" TypeName="State" SelectMethod="GetStates">
<asp:ControlParameter ControlID="dropDownListCountry" PropertyName="SelectedValue" Name="countryID" Type="Int32" DefaultValue="225" />
<asp:DropDownList ID="dropDownListState" runat="server" Width="99%" SelectedValue='<%# Bind("StateID") %>' DataSourceID="objStates" DataTextField="Name" DataValueField="ID" />
As you can imagine, the Dropdownlist for countries posts back, which should cause the page to reload and re-populate the ObjectDataSource for states using the new selected value of the country as a parameter. However, upon reloading, the page throws the following exception:
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
This method is working fine on a page with a manually-created form, but within a parent control, such as a DetailsView, this error is being thrown. Has anyone managed something like this, or can anyone offer any advice on the matter? Thanks.