[英]Asp .net dropdownlist SelectedItem doesn't change after clear in dropdownlist
[英]change selecteditem of dropdownlist after binding
我在頁面中有一個droplist,我將其與代碼綁定在一起
Category catObj = new Category();
dropCat.DataSource = catObj.GetAllCategory();
dropCat.DataTextField = "Title";
dropCat.DataValueField = "CategoryID";
dropCat.DataBind();
我希望用代碼更改droplist的選定項目
dropCat.SelectedIndex = Convert.ToInt32(catObj.ParentId);
但是此代碼無法更改所選項目,請幫助我謝謝大家
我假設您嘗試按值而不是按索引進行選擇,因為catObj.ParentId
可能包含CategoryId。 如果我的分裂是正確的,你需要這樣做
dropCat.SelectedValue = catObj.ParentId.ToString();
DropDownList.SelectedIndex
屬性引用Items集合內的位置,而不是項目的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.