如何使用 C# 在 asp.net 中设置 Dropdownlist 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14747046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:54:00  来源:igfitidea点击:

How to set Dropdownlist value in asp.net using C#

c#asp.netdictionary

提问by syed Ahsan Jaffri

i have a method to populate the dropdownlist in asp.net using C#

我有一种方法可以使用 C# 在 asp.net 中填充下拉列表

public void get_country_box_populated(ref System.Web.UI.WebControls.DropDownList dropDown, bool add_initial_text)
{
    dropDown.Items.Clear();
    //dropDown.Items.Add(0,"Select Any Country");
    var context = new db_vmartEntities();
    var query = from c in context.tbl_countary
                where c.status == true
                select new { c.countary_id, c.countary_name };

    var dictionary = new Dictionary<int, string>();
    if (add_initial_text)
    {
        dictionary.Add(0, "Select Any Country");
    }
    foreach (var item in query)
    {
        dictionary.Add(item.countary_id, item.countary_name);
    }
    dropDown.DataTextField = "Value";
    dropDown.DataValueField = "Key";
    dropDown.DataSource = dictionary;  //Dictionary<int, string>
    dropDown.DataBind();
}

now i need to select a default value on edit page something like this.

现在我需要在像这样的编辑页面上选择一个默认值。

store_registration my_store = str.get_store_by_id(Session["user"].ToString(), sid);
c.get_country_box_populated(ref countary_box,false);
countary_box.Text = countary_box.Items.FindByValue(my_store.countary).ToString();

but value in not set because patteren is like this

但未设置值,因为模式是这样的

Dictionary<key,value>
Dictionary<5,Pakistan>
Dictionary<8,India>
Dictionary<9,Iran>
Dictionary<6,UK>

any help or guide if i can set UK in dropdownlist when mystore.country value is 6

当 mystore.country 值为 6 时,如果我可以在下拉列表中设置 UK 有任何帮助或指南

回答by gzaxx

First of all you don't need to pass ComboBoxby reference.

首先,您不需要ComboBox通过引用传递。

To select value of DataBoudComboBox do this:

要选择DataBoudComboBox 的值,请执行以下操作:

countary_box.SelectedValue = my_store.countary_id; //im not 100% sure that this is the key, so change it to equivalent of item.countary_id

And it will preselect give value.

它会预先选择给值。

回答by Tim Schmelter

I assume the problem is in this line:

我认为问题出在这一行:

countary_box.Text = countary_box.Items.FindByValue(my_store.countary).ToString();

In ASP.NET you can set the SelectedValueproperty via the Textpropertyof a DropDownList(like in Winforms). Note the difference, you set the value by the text property. But ListItem.ToStringreturns the text not the value property.

在ASP.NET中,您可以设置SelectedValue通过属性Text特性DropDownList(如在的WinForms)。注意区别,您通过 text 属性设置值。但ListItem.ToString返回文本而不是 value 属性。

So you need this:

所以你需要这个:

countary_box.Text = my_store.countary.ToString(); // if countary is the int which is used as key 

or the same using SelectedValuedirectly:

或同样SelectedValue直接使用:

countary_box.SelectedValue = my_store.countary.ToString();