[英]Setting dropdownlist selecteditem programmatically
I want to set the selecteditem
attribute for an ASP.Net dropdownlist control programmatically.我想以编程方式为 ASP.Net 下拉列表控件设置
selecteditem
属性。
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.所以我想给下拉列表控件传递一个值来设置选中项,其中该项等于传递的值。
Assuming the list is already data bound you can simply set the SelectedValue
property on your dropdown list.假设列表已经是数据绑定的,您可以简单地在下拉列表中设置
SelectedValue
属性。
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. myValue
变量的值需要存在于控件数据绑定中DataValueField
中指定的属性中。
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.更新:如果
myValue
的值不作为具有下拉列表选项的值存在,它将默认选择下拉列表中的第一个选项。
ddlData.SelectedIndex
will contain the int
value To select the specific value into DropDown
: ddlData.SelectedIndex
将包含int
值要将特定值选择到DropDown
中:
ddlData.SelectedIndex=ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));
return
type of ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));
ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));
的return
类型; is int
.是
int
。
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"));
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.仅当该值存在于下拉列表的数据源中时,这才有效。
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");
}
Just Use this oneliner:只需使用这个 oneliner:
divisions.Items.FindByText("Some Text").Selected = true;
divisions.Items.FindByValue("some value").Selected = true;
where divisions is a dropdownlist control.其中 divisions 是一个下拉列表控件。
Hope it helps someone.希望它能帮助别人。
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!应该工作.. 特别是在使用 FindByText/Value 甚至不存在的扩展 RAD 控件时!
ddList.Items.FindByText("oldValue").Selected = false;
ddList.Items.FindByText("newValue").Selected = true;
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.在加载我的 Windows 窗体时,
comboBox
将显示我的DataTable
的ClassName
列,因为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
}
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;
ddlemployee.DataSource = ds.Tables[0];
ddlemployee.DataTextField = "Employee Name";
ddlemployee.DataValueField = "RecId";
ddlemployee.DataBind();
ddlemployee.Items.Insert(0, "All");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.