简体   繁体   English

绑定后更改下拉列表的选择项

[英]change selecteditem of dropdownlist after binding

i have a droplist in page that i bind this with code 我在页面中有一个droplist,我将其与代码绑定在一起

 Category catObj = new Category();
    dropCat.DataSource = catObj.GetAllCategory();
    dropCat.DataTextField = "Title";
    dropCat.DataValueField = "CategoryID";
    dropCat.DataBind();

i want change selected item of droplist with ths code 我希望用代码更改droplist的选定项目

dropCat.SelectedIndex = Convert.ToInt32(catObj.ParentId);

but this code cant change the selected item please help me thank you all 但是此代码无法更改所选项目,请帮助我谢谢大家

I'll assume that you're trying to select by value and not by index since catObj.ParentId is likely to contain a CategoryId. 我假设您尝试按值而不是按索引进行选择,因为catObj.ParentId可能包含CategoryId。 In case my assuption is correct, you'll need to do this 如果我的分裂是正确的,你需要这样做

dropCat.SelectedValue = catObj.ParentId.ToString();

DropDownList.SelectedIndex property refers to a position inside Items collection and not about the value of the item. DropDownList.SelectedIndex属性引用Items集合内的位置,而不是项目的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp .net 下拉列表 SelectedItem 在下拉列表中清除后不会更改 - Asp .net dropdownlist SelectedItem doesn't change after clear in dropdownlist WPF DataGrid SelectedItem绑定在项目更改后停止工作 - WPF DataGrid SelectedItem binding stops working after item change 更改WPF ListView SelectedItem绑定值 - Change WPF ListView SelectedItem binding value 为什么 ASP.NET DropDownList 更改侦听器“SelectedItem”是旧项目? - Why ASP.NET DropDownList Change listener "SelectedItem" is old item? 以编程方式设置下拉列表选定项 - Setting dropdownlist selecteditem programmatically ToolStripComboBox.SelectedItem更改不会传播到绑定源 - ToolStripComboBox.SelectedItem change does not propagate to binding source 通过一次更改将按钮 CommandParameter 绑定到 DataGrid SelectedItem - Binding Button CommandParameter to DataGrid SelectedItem off by one change 在 UnitOfWork.SaveChanges 之后 DataGrid 的 SelectedItem 丢失了绑定 - DataGrid´s SelectedItem lost Binding after UnitOfWork.SaveChanges DataGridListView's SelectedItem null 在MVVM中绑定后 WPF C# - DataGridListView's SelectedItem null in MVVM after binding WPF C# 与数据库绑定后在下拉列表中添加默认值 - Adding a default value in dropdownlist after binding with database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM