Set SelectedIndex of DropDownList based on return value from database
Posted on 2006-04-18
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 VB.net code.