Set SelectedIndex of DropDownList based on return value from database


I am trying to set the SelectedIndex of a DropDownList based upon a value that is returned from a Stored Procedure. The problem is that my current method doesn't account for a different # of items in the DropDownList.

Here's the basic setup:

ddlUserRoles - filled by a SQL SPROC that returns between 2 and 4 items based upon the role of the user visiting the page. An administrator would see 4 options while a 1st level Manager would only see 2, etc. Currently, I use a Case statement that assigns the SelectedIndex based upon the value I get out of the call.

Example: Joe is a 1st level manager.  He is going to edit the properties of one of his employees, Jane. The two possible RoleID options for Jane are 2 and 3. The DDL is populated with these two values. Janes' current RoleID is 3, so I would like for the RoleID of 3 record to get the SelectedIndex.

Is there some sort of FindByControl.value that will accomplish the same? And if so, any examples would be very helpful.  By the way, this is code.

Who is Participating?
Try this:

Dim iSelected as integer = Cint(ddlUserRoles.SelectItem.Value)
DDLTWO.Items.FindByValue(iSelected).Selected = True
SWROAuthor Commented:
That worked! Excellent.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.