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

Tool to identify unused css definitions

css optimization code-cleanup

Are there any good tools to help identify unused css definitions in project? A bunch of css files were pulled in and now I'm trying to clean things up a bit.

PyCUDA using struct properly

python c cuda indexoutofboundsexception pycuda

I am trying to implement a struct in my Pycuda code but i am getting out of bounds errors. I tried following this tutorial but am unable to get it working for my case.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.