AJAX cascading dropdownlists in a FormView

Last post 01-11-2008 10:25 AM by nyinazz. 3 replies.

Sort Posts:

  • AJAX cascading dropdownlists in a FormView

    04-06-2007, 6:38 AM

    Hi all,

    I'm trying to create a set of cascading DropDownLists within a BOUND FormView.

    <asp:FormView ID="FormView1" ....>

    <EditItemTemplate>

    Program Mode:

    <asp:DropDownList ID="ProgramModeID" SelectedValue='<%# Bind("ProgramModeID") %>'
        runat="server" DataSourceID="dsProgramMode" DataTextField="ProgramModeDesc" DataValueField="ProgramModeID"
        AppendDataBoundItems="true" OnSelectedIndexChanged="Program_SelectedIndexChanged"
        AutoPostBack="true">
    </asp:DropDownList>
    <asp:SqlDataSource ...>

    Program Name:

    <ajax:UpdatePanel ID="pnlProgramName" OnPreRender="FormView1_DataBinding" runat="server">
        <Triggers>
            <ajax:AsyncPostBackTrigger ControlID="ProgramCategoryID" EventName="SelectedIndexChanged" />
            <ajax:AsyncPostBackTrigger ControlID="ProgramModeID" EventName="SelectedIndexChanged" />
        </Triggers>
        <ContentTemplate>
            <asp:DropDownList ID="ProgramNameID" SelectedValue='<%# Bind("ProgramNameID") %>' runat="server" DataSourceID="dsProgramName"
                DataTextField="ProgramNameDesc" DataValueField="ProgramNameID" AppendDataBoundItems="false">
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsProgramName" runat="server" ConnectionString="<%$ ConnectionStrings:SqlServer %>">
            </asp:SqlDataSource>
        </ContentTemplate>
    </ajax:UpdatePanel>

    Here's what I'm trying to do:

    Program Name dropdownlist (DDL) should change depending on what is selected in Program Mode DDL.

    The events Program_SelectedIndexChanged and FormView1_DataBinding both call this function in the codebehind:

    protected void GetdsProgramName()
        {
            SqlDataSource dsProgramName = (SqlDataSource)FormView1.FindControl("dsProgramName");
            DropDownList ProgramNameID = (DropDownList)FormView1.FindControl("ProgramNameID");

            if (dsProgramName != null && ProgramNameID != null)
            {
                string SelectCommand = "SELECT ...";
                dsProgramName.SelectCommand = SelectCommand;
                ProgramNameID.DataBind();
            }
        }

     My problem is that I keep getting the error:

    'ProgramNameID' has a SelectedValue which is invalid because it does not exist in the list of items.

    every time I go into the FormView. I think it's because I'm not calling the GetdsProgramName function early enough and the Binding is going ahead before the ProgramNameID DDL has been bound to the list of data. If I put the same SelectCommand within the <SqlDataSource> tag, it works fine.

    When should I set the SelectCommand (and databind) the DDL? On which event? I'm super confused when it comes to this Bind command of FormView, because I can never figure out when the Bind takes place.

    Would greatly appreciate any help. Thanks.
     

    Filed under: , ,
  • Re: AJAX cascading dropdownlists in a FormView

    05-04-2007, 5:16 PM
    • Loading...
    • perdigano
    • Joined on 04-24-2007, 11:40 AM
    • Posts 3

    I've got a related problem caused by the AJAX CascadedDropDownList. I don´t know why but when you do  a databinding using the "SelectedValue" property, this always get the string:  "Value_of_Item_Selected:::Name_of_item_selected".

    I had to manage the event in the code behind the page before saving/modifying/inserting a record by extracting from that "strange formatted string" the real value selected. Maybe a bug?

  • Re: AJAX cascading dropdownlists in a FormView

    05-04-2007, 5:19 PM
    • Loading...
    • perdigano
    • Joined on 04-24-2007, 11:40 AM
    • Posts 3

    I've got a related problem caused by the AJAX CascadedDropDownList. I don´t know why but when you do  a databinding using the "SelectedValue" property, this always get the string:  "Value_of_Item_Selected:::Name_of_item_selected".

    I had to manage the event in the code behind the page before saving/modifying/inserting a record by extracting from that "strange formatted string" the real value selected. Maybe a bug?

  • Re: AJAX cascading dropdownlists in a FormView

    01-11-2008, 10:25 AM
    • Loading...
    • nyinazz
    • Joined on 08-16-2007, 10:56 AM
    • Posts 7

    Is this a bug? I have come across the same thing and I was going nuts. Is'nt there a solution other than manually editing the data before updating?

    Please let me know if you know of any solution to this.

    Thanks.

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter