Populating a dropdown with AJAX

I have 3 dropdowns created using the select HTML tag. When loading a page, there are several names in the first field. Now, when I click on one of the names in the first field, other names appear in the second, and when I click on the name in the second field, other names should appear in the third field. How can I achieve this using AJAX ? I have to use ASP.Net and MS SQL Server. I am a complete noob for AJAX, and I learned about it, but nothing worked. I searched for the code for about a week. I looked at w3schools.com, but when I tried this code, it did not work. Please help me and please tell me step by step what is needed to make it work and what is going on there. I have a deadline that is fast approaching, and I am in my mind trying to get it to work. HELP ME !!

+5
source share
1 answer

dropdownlists UpdatePanel . , , . , .

, , .


" ", , . , :

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MultiDropDown.aspx.cs" Inherits="AppSettingsRetrieval.MultiDropDown" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                String[] options = new[] { "ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VWX", "YZA" };
                foreach (String option in options)
                {
                    this.DropDownList1.Items.Add(new ListItem(option, option));
                }
            }
        }

        public void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DropDownList2.Items.Clear();
            this.DropDownList2.Items.Add(new ListItem("--- Please Select One ---"));

            String[] options = new[] { "123", "456", "789", "101", "112", "131", "415", "161", "718" };
            foreach (String option in options)
            {
                var str = String.Format("{0} {1}", this.DropDownList1.SelectedValue, option);
                this.DropDownList2.Items.Add(new ListItem(str, str));
            }
            this.DropDownList2.Enabled = true;

            this.DropDownList3.Enabled = false;
        }

        public void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DropDownList3.Items.Clear();
            this.DropDownList3.Items.Add(new ListItem("--- Please Select One ---"));

            String[] options = new[] { "test", "testing", "tester", "foo", "bar", "foobar" };
            foreach (String option in options)
            {
                var str = String.Format("{0} {1}", this.DropDownList2.SelectedValue, option);
                this.DropDownList3.Items.Add(new ListItem(str, str));
            }
            this.DropDownList3.Enabled = true;
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>

    <asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
        <ContentTemplate>
            <fieldset>
                <legend>Consecutive Dropdown List</legend>
                <p>
                    Primary Filter:
                    <asp:DropDownList runat="server" ID="DropDownList1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
                        <asp:ListItem Text="---Please Select One---" />
                    </asp:DropDownList>
                </p>
                <p>
                    Secondary Filter:
                    <asp:DropDownList runat="server" ID="DropDownList2" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" Enabled="false">
                        <asp:ListItem Text="---Please Select One---" />
                    </asp:DropDownList>
                </p>
                <p>
                    Final Filter:
                    <asp:DropDownList runat="server" ID="DropDownList3" Enabled="false">
                        <asp:ListItem Text="---Please Select One---" />
                    </asp:DropDownList>
                </p>
            </fieldset>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
+7

All Articles