karthik gorijavolu
karthik gorijavolu

Reputation: 860

Populating Drop down dynamically using ASP.NET

In one of my form there were two Dropdown fields. The second dropdown has to be populated from the database, dynamically from the selection of first dropdown.

Any help is appreciated.

Upvotes: 0

Views: 129

Answers (1)

Jupaol
Jupaol

Reputation: 21365

In the SelectedIndexChanged event of the first DropDownList, add code to populate the second DropDownList based on the selected value of the first list

Like this:

<asp:DropDownList runat="server" ID="from" AutoPostBack="true" CausesValidation="false" OnSelectedIndexChanged="from_SelectedIndexChanged">
</asp:DropDownList>

<asp:DropDownList runat="server" ID="to" />

    protected void from_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedValue = this.from.SelectedValue;

        this.to.DataSource = /*get the data of the second list based on: selectedValue*/;
        this.to.DataBind();
    }

Upvotes: 3

Related Questions