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
但是值在not set中,因为patteren是这样的
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下拉列表中设置UK,任何帮助或指导。国家值是6
2 个解决方案
#1
2
First of all you don't need to pass ComboBox
by reference.
首先,不需要通过引用传递ComboBox。
To select value of DataBoud
ComboBox do this:
要选择DataBoud ComboBox的值,请执行以下操作:
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.
它会预先选择赋予价值。
#2
0
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 SelectedValue
property via the Text
property of a DropDownList
(like in Winforms). Note the difference, you set the value by the text property. But ListItem.ToString
returns the text not the value property.
在ASP。NET可以通过DropDownList的文本属性(如Winforms)设置SelectedValue属性。注意区别,您通过文本属性设置值。但列。ToString返回的是文本而不是值属性。
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 SelectedValue
directly:
或直接使用SelectedValue:
countary_box.SelectedValue = my_store.countary.ToString();
#1
2
First of all you don't need to pass ComboBox
by reference.
首先,不需要通过引用传递ComboBox。
To select value of DataBoud
ComboBox do this:
要选择DataBoud ComboBox的值,请执行以下操作:
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.
它会预先选择赋予价值。
#2
0
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 SelectedValue
property via the Text
property of a DropDownList
(like in Winforms). Note the difference, you set the value by the text property. But ListItem.ToString
returns the text not the value property.
在ASP。NET可以通过DropDownList的文本属性(如Winforms)设置SelectedValue属性。注意区别,您通过文本属性设置值。但列。ToString返回的是文本而不是值属性。
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 SelectedValue
directly:
或直接使用SelectedValue:
countary_box.SelectedValue = my_store.countary.ToString();