【问题标题】:Selecting value that binded to a dropdownlist in C#在 C# 中选择绑定到下拉列表的值
【发布时间】:2018-05-11 14:24:05
【问题描述】:

我使用此代码将值和文本绑定到 C# 中下拉列表的 DataValueField 和 DataTextField,如下所示。

private void fillCombo()
{
    DataSet Ds = new DataSet();
    Ds = BL.fillCombo();
    if(Ds.Tables.Count>0)
    {
        cboDesg.DataSource = Ds.Tables[0];
        cboDesg.DataValueField = Ds.Tables[0].Columns[0].ColumnName;
        cboDesg.DataTextField = Ds.Tables[0].Columns[1].ColumnName;
        cboDesg.DataBind();
    }
}

但是当我试图从这个下拉列表中选择一个项目时,只有第一个值总是被选中。我使用了以下代码。

String empDsg = cboDesg.SelectedValue.ToString();

给我一​​个建议。

【问题讨论】:

  • 看看this answer对你有帮助吗?
  • 我认为您的页面每次都会更新并选择默认值。您在使用更新面板吗?你想在哪里获得价值你能分享整个功能吗?还有一个问题是您要设置还是获取值?
  • 是的,这就是问题所在。我未能进行 !Ispostback 检查。谢谢你们

标签: c# drop-down-menu


【解决方案1】:

首先确保页面没有被回发或使用更新面板。

根据值设置下拉列表:

dropdownlist.ClearSelection(); //to clear previous selection
dropdownlist.Items.FindByValue(value).Selected = true;

获取下拉列表选择的值:

string textvalue = DropdownList1.SelectedItem.Value; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-03
    • 2019-02-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多