Dropdownlist selected value at Selectedindexchange

2019-04-06 21:48发布

I'm working on asp.net website with Vb.net and I have a dropdownlist with autopostback = true and I need to get the selected value when I change the item or I want to get the item which fires the selectedindexchanged event ..

any help please..

3条回答
孤傲高冷的网名
2楼-- · 2019-04-06 22:11

In ie. your Page_Load set

this.ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);

Then write the event handler like this:

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
  ComboBox comboBox = (ComboBox) sender;
  string selected = (string) comboBox.SelectedItem;
}

Make sure that in your Page_Load you write this before setting the combobox default value or you will end up with this always being the selected item:

if (Page.IsPostBack)
  return;
查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-06 22:22

try this:

    protected void list_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;
        string value = (string)list.SelectedValue;
    }
查看更多
Bombasti
4楼-- · 2019-04-06 22:28

If item is a Dictionary:

string value = ((KeyValuePair<string, string>)combobox.SelectedItem).Key;
查看更多
登录 后发表回答