在Selectedindexchanged事件中选择下拉列表值

时间:2012-02-21 02:09:59

标签: asp.net drop-down-menu selectedindexchanged

我正在使用Vb.net在asp.net网站上工作,我有一个autopostback = true的下拉列表,我需要在更改项目时获取所选值,或者我想获取触发该项目的项目selectedindexchanged event ..

任何帮助请...

3 个答案:

答案 0 :(得分:8)

在ie。你的Page_Load集

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

然后像这样编写事件处理程序:

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

在设置组合框默认值之前,请确保在您的Page_Load中编写此内容,否则您最终会选择此项:

if (Page.IsPostBack)
  return;

答案 1 :(得分:8)

试试这个:

    protected void list_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;
        string value = (string)list.SelectedValue;
    }

答案 2 :(得分:0)

如果item是Dictionary:

string value = ((KeyValuePair<string, string>)combobox.SelectedItem).Key;