ASP.NET - Set index of drop-down list based on value.

I've got a page with a dropdownlist on it.  When the page is requested, there is a value passed in the url query string that represents the value of one of the list items of the drop-down list.  As the page loads I'm trying to set the selected item of the drop-down list based on the value that is being passed in.

url query string is something like:
http://www.xyz.com/page.aspx?WHEREID=37

attempted code:
this code throws an error saying that FindByValue() cannot be converted.
ddlSelectLevel.SelectedIndex = Convert.ToInt32(ddlSelectLevel.Items.FindByValue(Request("WHEREID")))
josephdaviskcrmAsked:
Who is Participating?
 
ororioleConnect With a Mentor Commented:
FindByValue returns an item, not a string. use:
ddlSelectLevel.SelectedIndex = ddlSelectLevel.IndexOf(ddlSelectLevel.Items.FindByValue(Request("WHEREID")))
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
It should be:

ddlSelectLevel.SelectedIndex = Convert.ToInt32(ddlSelectLevel.Items.FindByValue(Request.QueryString("WHEREID")))
0
 
josephdaviskcrmAuthor Commented:
No, (Request("WHEREID") returns the value I'm needing.  The problem lies somewhere in the rest of the line of code.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Jorge PaulinoIT Pro/DeveloperCommented:
Sorry you're right!
0
 
ororioleCommented:
Hey even a blind squirrel like me finds a nut once in a while. :)
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
:)
0
 
josephdaviskcrmAuthor Commented:
The dropdownlist server control has no .IndexOf() method, unless it is in a higher version of the .NET framework than I am using, which is 2.0.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Can you try ?

ddlSelectLevel.SelectedValue = ddlSelectLevel.Items.FindByValue(Request("WHEREID")).Value
0
 
josephdaviskcrmAuthor Commented:
Ok... I figured it out...   Its like you said ororiole but with one small change. its ddlSelectLevel.Items.IndexOf()

ddlSelectLevel.SelectedIndex = ddlSelectLevel.Items.IndexOf(ddlSelectLevel.Items.FindByValue(Request("WHEREID")))
0
 
josephdaviskcrmAuthor Commented:
its ddlSelectLevel.Items.IndexOf()
0
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.