Re-Binding A DropDownList Within A DetailsView

Discussion in 'ASP .Net' started by 47computers@gmail.com, Mar 23, 2007.

  1. Guest

    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
     
    , Mar 23, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2VyaV9EYXZpZXM=?=

    Binding a textbox to a detailsview in asp.net 2.0

    =?Utf-8?B?Q2VyaV9EYXZpZXM=?=, Mar 22, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    995
    =?Utf-8?B?Q2VyaV9EYXZpZXM=?=
    Mar 22, 2005
  2. JJ
    Replies:
    1
    Views:
    5,236
  3. viktor
    Replies:
    0
    Views:
    543
    viktor
    Aug 13, 2007
  4. Feege
    Replies:
    0
    Views:
    485
    Feege
    Dec 20, 2005
  5. Madison

    Dropdownlist within DetailsView

    Madison, Jun 10, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    153
    Madison
    Jun 10, 2008
Loading...

Share This Page