Question

I am working on a website in ASP.NET and ran into a problem. I want the website to add options to one drop down list when a new option is selected in another drop down list. To do this, I am using the second drop down list's SelectedIndexChanged event. When I test the code by adding this line into this event:

DropDownSubject.Items.Add("TEST");

nothing happens to the first drop down list. Is it because this drop down list has the words "Unbound" written in them automatically in the editor? How can I fix this problem?

Here is the markup for the first drop down list, which I want to add items to:

<asp:DropDownList ID="DropDownClasses" runat="server" 
                >

and the second, which I am using the event with:

<asp:DropDownList ID="DropDownSubject" runat="server" 
                onselectedindexchanged="DropDownSubject_SelectedIndexChanged">
                <asp:ListItem>Mathematics</asp:ListItem>
                <asp:ListItem>Foreign Language</asp:ListItem>
                <asp:ListItem>Science</asp:ListItem>
                <asp:ListItem>Social Studies</asp:ListItem>
                <asp:ListItem>English</asp:ListItem>
            </asp:DropDownList>

All help is greatly appreciated.

Was it helpful?

Solution

Details: Sounds like you don't have AutoPostBack set for the first drop down list. Example below

  <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" 
    AutoPostBack="true">
        <asp:ListItem>Cat</asp:ListItem>
        <asp:ListItem>Dog</asp:ListItem>
  </asp:DropDownList>

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


Code Behind

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList2.Items.Add(DropDownList1.SelectedItem.Text);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top