Search code examples
c#asp.netasp.net-ajaxdrop-down-menuautopostback

Populating a DropDownList from another DropDownList using AJAX 3.5


I am using asp.net 3.5 with the ajax toolkit.

The problem: I have a custom control with two drop down lists in an update panel. The first DDL has the property AutoPostBack="true" and upon selection the second DDL is populated. The issue is the first time after initial page load the DDL is selected, the entire page reloads. The second time an item in the first DDL is selected, everything works as expected.

I have tried adding Triggers in the UpdatePanel, but that does not change the outcome.

Any help is appreciated.

.ascx:

<asp:UpdatePanel ID="popDates" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <p>
                    <asp:DropDownList ID="ddlDivision" runat="server" AutoPostBack="true" style="width:300px"></asp:DropDownList>
                    <asp:RequiredFieldValidator ID="requiredDivision" runat="server" 
                                ControlToValidate="ddlDivision" ErrorMessage="* Please specify a value" 
                                ValidationGroup="valGroupGetDates"
                                InitialValue="Select..." SetFocusOnError="True" CssClass="formValidation">
                    </asp:RequiredFieldValidator>
                </p>
                <p>
                    <asp:DropDownList ID="ddlKMA" runat="server" Enabled="False" AutoPostBack="true" style="width:300px"></asp:DropDownList>
                    <asp:RequiredFieldValidator ID="requiredKMA" runat="server" 
                                ControlToValidate="ddlKMA" ErrorMessage="* Please specify a value" 
                                ValidationGroup="valGroupGetDates"
                                InitialValue="Select..." SetFocusOnError="True" CssClass="formValidation">
                    </asp:RequiredFieldValidator>
                </p>
            </ContentTemplate>
            </asp:UpdatePanel>

ascx.cs:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsAsync || Page.IsPostBack)
            {
                String target = Page.Request.Params.Get("__EVENTTARGET");
                //Division Session                
                Session["divisionDropDown"] = ddlDivision.SelectedItem.Value;
                populateDivisionDDL();
                ddlDivision.SelectedValue = Session["divisionDropDown"].ToString();

                if (target != "" && target != null)
                {
                    if (target.Contains("ddlDivision"))
                    {
                        populateKMA(ddlDivision.SelectedValue);
                    }
             }
            }

            if (!Page.IsPostBack)
            {
                populateDivisionDDL();
                ddlKMA.Items.Clear();
                ddlKMA.Items.Add(default_item());
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
    }

aspx:

<body>
    <form id="ViewSPANodeDatesForm" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManagerDates" runat="server"></asp:ScriptManager>
        <viewControl:SPANodeDates ID="SPANodeDates1" runat="server"></viewControl:SPANodeDates>
    </div>
    </form>
</body>

Note: Before adding the ajax controls, everything worked as expected.

Thanks!


Solution

  • here you have three examples of cascading dropdownlist using controller and webservice.

    http://stephenwalther.com/blog/archive/2008/09/07/asp-net-mvc-tip-41-creating-cascading-dropdown-lists-with-ajax.aspx

    I can paste the whole info but I believe the link will be better. even stephen offer code example of it. brgds.