gpt4 book ai didi

c# - 如何使用 C# 在 asp.net 中设置下拉列表值

转载 作者:行者123 更新时间:2023-11-30 17:07:12 25 4
gpt4 key购买 nike

我有一种使用 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();

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

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

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

最佳答案

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

要选择 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

它会预选给值。

关于c# - 如何使用 C# 在 asp.net 中设置下拉列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14747046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com