【问题标题】:Get DropDownList selected value [duplicate]获取 DropDownList 选定值 [重复]
【发布时间】:2025-12-25 22:10:06
【问题描述】:

我有一个 DropDownList 填写在 Page_Load 上。当我尝试获取所选值string id = myDropDownList.SelectedValue.ToString(); 时,每次我只得到第一个值,但是当我更改 DropDownList 的值并检查 string id 时,它总是显示我的第一个值。

这是我的 Page_Load

protected void Page_Load(object sender, EventArgs e)
    {
        if (commesseDataSource.Count != 0)
        {
            initComboCommesse();
        }

        if (Session["Matricola"] != null)
            matricolaDip = Convert.ToInt32(Session["Matricola"]);

        if (Session["LivLogin"] != null)
            livlogin = Convert.ToInt32(Session["Matricola"]);

    }

这就是我绑定 DropDown 的方式

private void initComboCommesse()
    {
        myDropDownList.DataTextField = "Value";
        myDropDownList.DataValueField = "Key";
        myDropDownList.DataSource = commesseDataSource;
        myDropDownList.DataBind();
    }

我已经试过了

 protected void myDropDownList_SelectedIndexChanged(object sender, EventArgs e)
 {
      string id = myDropDownList.SelectedValue.ToString(); // I always get the 1st value of the Drop Down
 }

还有

 protected void myDropDownList_TextChanged(object sender, EventArgs e)
 {
      string id = myDropDownList.SelectedValue.ToString(); // I always get the 1st value of the Drop Down
 }

但是这些都不起作用,我想知道如何获取 DropDownList 的选定值,每次我在 DropDownList 中选择一个新值时

【问题讨论】:

  • 还可以使用下拉标记更新您的问题
  • @ArunPratap 你想知道我如何填写下拉菜单吗?
  • @ChangeWorld 你能告诉我数据库 int 或 string 中的哪个值存储吗?
  • 你可能想做一些搜索;这是一个常见的问题。这通常是由于在重新填充下拉列表之前没有检查Page_Load 中的IsPostBack
  • @HereticMonkey 是的,做得很好!

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


【解决方案1】:

试试这个:

当您在数据库中插入 DropDownList 值时,您需要更改:

string id = myDropDownList.SelectedItem.Value.ToString();

【讨论】:

    最近更新 更多