以编程方式设置下拉列表选定项

发布于 2024-09-14 13:29:40 字数 108 浏览 3 评论 0原文

我想以编程方式设置 ASP.Net 下拉列表控件的 selecteditem 属性。

所以我想将一个值传递给下拉列表控件来设置所选项目,其中该项目等于传递的值。

I want to set the selecteditem attribute for an ASP.Net dropdownlist control programmatically.

So I want to pass a value to the dropdownlist control to set the selected item where the item is equal to the passed value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

骷髅 2024-09-21 13:29:40

假设列表已经是数据绑定的,您只需在下拉列表中设置 SelectedValue 属性即可。

list.DataSource = GetListItems(); // <-- Get your data from somewhere.
list.DataValueField = "ValueProperty";
list.DataTextField = "TextProperty";
list.DataBind();

list.SelectedValue = myValue.ToString();

myValue 变量的值需要存在于控件数据绑定的 DataValueField 内指定的属性中。

更新
如果 myValue 的值不作为下拉列表选项的值存在,它将默认选择下拉列表中的第一个选项。

Assuming the list is already data bound you can simply set the SelectedValue property on your dropdown list.

list.DataSource = GetListItems(); // <-- Get your data from somewhere.
list.DataValueField = "ValueProperty";
list.DataTextField = "TextProperty";
list.DataBind();

list.SelectedValue = myValue.ToString();

The value of the myValue variable would need to exist in the property specified within the DataValueField in your controls databinding.

UPDATE:
If the value of myValue doesn't exist as a value with the dropdown list options it will default to select the first option in the dropdown list.

╰ゝ天使的微笑 2024-09-21 13:29:40

ddlData.SelectedIndex 将包含 int 值 将特定值选择到 DropDown 中:

ddlData.SelectedIndex=ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));

return 类型的 ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));int

ddlData.SelectedIndex will contain the int value To select the specific value into DropDown :

ddlData.SelectedIndex=ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));

return type of ddlData.Items.IndexOf(ddlData.Items.FindByText("value")); is int.

夏尔 2024-09-21 13:29:40

这是我正在寻找的代码:

DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByText("PassedValue"));

或者

DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByValue("PassedValue"));

Here is the code I was looking for :

DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByText("PassedValue"));

Or

DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByValue("PassedValue"));
兮子 2024-09-21 13:29:40

好吧,如果我正确理解你的问题。为给定下拉列表设置值的解决方案是:

dropdownlist1.Text="Your Value";

仅当该值存在于下拉列表的数据源中时,此方法才有效。

Well if I understood correctly your question. The Solution for setting the value for a given dropdownlist will be:

dropdownlist1.Text="Your Value";

This will work only if the value is existing in the data-source of the dropdownlist.

空心↖ 2024-09-21 13:29:40

如果您需要根据表达式选择列表项:

foreach (ListItem listItem in list.Items)
{
    listItem.Selected = listItem.Value.Contains("some value");
}

If you need to select your list item based on an expression:

foreach (ListItem listItem in list.Items)
{
    listItem.Selected = listItem.Value.Contains("some value");
}
心是晴朗的。 2024-09-21 13:29:40

只需使用这个 oneliner:

divisions.Items.FindByText("Some Text").Selected = true;
divisions.Items.FindByValue("some value").Selected = true;

其中,divisions 是一个下拉列表控件。

希望它能帮助某人。

Just Use this oneliner:

divisions.Items.FindByText("Some Text").Selected = true;
divisions.Items.FindByValue("some value").Selected = true;

where divisions is a dropdownlist control.

Hope it helps someone.

鸠书 2024-09-21 13:29:40
var index = ctx.Items.FirstOrDefault(item => Equals(item.Value, Settings.Default.Format_Encoding));
ctx.SelectedIndex = ctx.Items.IndexOf(index);

或者

foreach (var listItem in ctx.Items)
  listItem.Selected = Equals(listItem.Value as Encoding, Settings.Default.Format_Encoding);

应该可以工作..特别是当使用扩展的 RAD 控件时,其中 FindByText/Value 甚至不存在!

var index = ctx.Items.FirstOrDefault(item => Equals(item.Value, Settings.Default.Format_Encoding));
ctx.SelectedIndex = ctx.Items.IndexOf(index);

OR

foreach (var listItem in ctx.Items)
  listItem.Selected = Equals(listItem.Value as Encoding, Settings.Default.Format_Encoding);

Should work.. especially when using extended RAD controls in which FindByText/Value doesn't even exist!

眼眸里的快感 2024-09-21 13:29:40
ddList.Items.FindByText("oldValue").Selected = false;
ddList.Items.FindByText("newValue").Selected = true;
ddList.Items.FindByText("oldValue").Selected = false;
ddList.Items.FindByText("newValue").Selected = true;
浸婚纱 2024-09-21 13:29:40

加载我的 Windows 窗体时,comboBox 将显示我的 DataTableClassName 列,因为它的 DisplayMember 也具有它的 ValueMember (对用户不可见)。

private void Form1_Load(object sender, EventArgs e)
            {
                this.comboBoxSubjectCName.DataSource = this.Student.TableClass;
                this.comboBoxSubjectCName.DisplayMember = TableColumn.ClassName;//Column name that will be the DisplayMember
                this.comboBoxSubjectCName.ValueMember = TableColumn.ClassID;//Column name that will be the ValueMember
            }

On load of My Windows Form the comboBox will display the ClassName column of my DataTable as it's the DisplayMember also has its ValueMember (not visible to user) with it.

private void Form1_Load(object sender, EventArgs e)
            {
                this.comboBoxSubjectCName.DataSource = this.Student.TableClass;
                this.comboBoxSubjectCName.DisplayMember = TableColumn.ClassName;//Column name that will be the DisplayMember
                this.comboBoxSubjectCName.ValueMember = TableColumn.ClassID;//Column name that will be the ValueMember
            }
想你的星星会说话 2024-09-21 13:29:40

安全检查仅选择项目是否匹配。

//try to find item in list.  
ListItem oItem = DDL.Items.FindByValue("PassedValue"));
//if exists, select it.
if (oItem != null) oItem.Selected = true;

Safety check to only select if an item is matched.

//try to find item in list.  
ListItem oItem = DDL.Items.FindByValue("PassedValue"));
//if exists, select it.
if (oItem != null) oItem.Selected = true;
有木有妳兜一样 2024-09-21 13:29:40

我认为这是最好的解决方案

drpGroups.SelectedValue = drpGroups.Items[0].Value.ToString();

i think this is the best solution

drpGroups.SelectedValue = drpGroups.Items[0].Value.ToString();

掩耳倾听 2024-09-21 13:29:40
            ddlemployee.DataSource = ds.Tables[0];
            ddlemployee.DataTextField = "Employee Name";
            ddlemployee.DataValueField = "RecId";
            ddlemployee.DataBind();
            ddlemployee.Items.Insert(0, "All");
            ddlemployee.DataSource = ds.Tables[0];
            ddlemployee.DataTextField = "Employee Name";
            ddlemployee.DataValueField = "RecId";
            ddlemployee.DataBind();
            ddlemployee.Items.Insert(0, "All");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文