我有一种使用 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();
}
现在我需要在编辑页面上选择一个默认值,就像这样。
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();
但未设置值,因为模式是这样的
Dictionary<key,value>
Dictionary<5,Pakistan>
Dictionary<8,India>
Dictionary<9,Iran>
Dictionary<6,UK>
如果我可以在 mystore.country 值为 6 时在下拉列表中设置 UK 的任何帮助或指导