Set SelectedIndex of DropDownList based on return value from database

Posted on 2006-04-18
Medium Priority
Last Modified: 2008-03-10

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.

Question by:SWRO
LVL 13

Accepted Solution

Torrwin earned 500 total points
ID: 16480370
Try this:

Dim iSelected as integer = Cint(ddlUserRoles.SelectItem.Value)
DDLTWO.Items.FindByValue(iSelected).Selected = True

Author Comment

ID: 16481644
That worked! Excellent.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question