0

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.

3
  • what does "Nothing happens to the first drop down list" mean? does it mean that SelectedIndexChanged event is not firing or SelectedIndexChanged is firing but cannot add the item? Commented Nov 20, 2012 at 3:49
  • please, show markup for both dropdowns Commented Nov 20, 2012 at 3:53
  • It appears that neither the event is firing or the item is being added to the drop down list. Commented Nov 20, 2012 at 3:55

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.