[英]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.