简体   繁体   中英

change selecteditem of dropdownlist after binding

i have a droplist in page that i bind this with code

 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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM