How do I populate a dropdownlist based on a selection from another dropdownlist?

protected void ddlEnvironment_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlEnvironment.SelectedIndex == 0)
    {
        ddlServers.Items.Add("item1");
    }

    if (ddlEnvironment.SelectedIndex == 1)
    {
        ddlServers.Items.Add("item2");
    }

    if (ddlEnvironment.Text == "Production")
    {
    }
}

The above is not working. When I make a selection on ddlEnvironment and it is the first item on the list (index 0), the other dropdownlist is not upading with "item1". Why?

Answers


There could be different reasons by default dropdown list do no do postback.

  • Check if you have AutoPostBack="true"
  • You bind the ddlEnvironment in !Page.IsPostBack block so that it maintains its state on postback

    if(!Page.IsPostBack)
    {
       ddlEnvironment.AuutoPostBack = true;
       ddlEnvironment.DataSource = datasource; 
       ddlEnvironment.DataBind();
    }
    

Need Your Help

Which On-Screen Keyboard for Touch Screen Application?

c++ user-interface windows-xp touchscreen

I'm developing an application in C++ that's partially driven by touch-screen on Windows XP Embedded. Some text entry will be necessary for the user. So far we've been using the standard Windows

How do I find the lockholder (reader) of my ReaderWriterLock in windbg

.net windbg deadlock readerwriterlock

I've got a dump of a .Net process that has hung due to a deadlock (the gui thread is no longer responding, and my logs show that some threads have stopped responding). I have taken a snapshot and a...

Is it possible to create a checkbox with images along with the text in the labels in cakephp?

cakephp-2.0

Somebody please give me ideas on this!I would like to generate multiple checkboxes with an image along with the text in the label. I have created a field called solutioncheckbox in the Contact form...