获取DropDownList选定值

时间:2020-02-19 14:38:26

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

我有一个 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的选定值

1 个答案:

答案 0 :(得分:0)

尝试一下:

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

string id = myDropDownList.SelectedItem.Value.ToString();
相关问题