如何使用C#在ASP.NET中设置Dropdownlist值 [英] How to set Dropdownlist value in asp.net using C#
本文介绍了如何使用C#在ASP.NET中设置Dropdownlist值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种方法可以使用C#在asp.net中填充下拉列表
i have a method to populate the dropdownlist in asp.net using C#
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>
在mystore.country值为6时是否可以在下拉列表中设置英国的任何帮助或指南
any help or guide if i can set UK in dropdownlist when mystore.country value is 6
推荐答案
首先,您不需要通过引用传递 ComboBox
.
First of all you don't need to pass ComboBox
by reference.
要选择 DataBoud
ComboBox的值,请执行以下操作:
To select value of DataBoud
ComboBox do this:
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
它将预选给定值.
这篇关于如何使用C#在ASP.NET中设置Dropdownlist值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文