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

Amr*_*har 8 asp.net selectedindexchanged drop-down-menu

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

任何帮助请..

Sti*_*ian 8

在ie.你的Page_Load集

this.ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);
Run Code Online (Sandbox Code Playgroud)

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

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

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

if (Page.IsPostBack)
  return;
Run Code Online (Sandbox Code Playgroud)


小智 8

试试这个:

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

  • 这给了我一个 SelectedValue ,无论初始设置是什么,而不是新选择的设置。 (2认同)