简体   繁体   中英

Replacing string in dropdown menu

So im trying to load a different string when the user has selected a different item, my code:

void ModeSelectorSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var data1 = new string[]
    {
        "January 2012",
        "February 2012",
        "March 2012",
        "April 2012",
    };

    var data2 = new string[]
    {
         "Married",
         "Divorced",
         "Buy new house",
         "Get promotion",
    };

    if (_Menu2.SelectedIndex == 2) 
    {
        _Menu3.ItemsSource = data1;     
    }

    else 

    if (_Menu2.SelectedIndex == 3)
    {

        _Menu3.ItemsSource = data2;
    }
}

When I use just 1 string it works fine, but as soon as I try to load 2 different strings in the same drop down menu it shuts down, meaning that its not showing any string data at all. What am I doing wrong?

For ASP.NET WebForms DropDownList follow this code snippet:

_Menu3.DataSource = someData;
_Menu3.DataBind();

Perhaps it would be better to move this logic on a client side and use javascript.

Try this code. It's work for me and AutoPostBack will be true for _Menu2.

protected void ModeSelectorSelectionChanged(object sender, EventArgs e)
{
    var data1 = new string[]
                                    {
                                    "January 2012",
                                    "February 2012",
                                    "March 2012",
                                    "April 2012",
                                    };

    var data2 = new string[]
                                    {
                                    "Married",
                                    "Divorced",
                                    "Buy new house",
                                    "Get promotion",
                                    };
    if (_Menu2.SelectedIndex == 2)
    {
        _Menu3.DataSource = data1;
        _Menu3.DataBind();

    }
    else if (_Menu2.SelectedIndex == 3)
    {

        _Menu3.DataSource = data2;
        _Menu3.DataBind();
    }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM