Solved

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

Posted on 2008-06-12
10
2,710 Views
Last Modified: 2013-11-26
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")))
0
Comment
Question by:josephdaviskcrm
  • 4
  • 4
  • 2
10 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 21771603
It should be:

ddlSelectLevel.SelectedIndex = Convert.ToInt32(ddlSelectLevel.Items.FindByValue(Request.QueryString("WHEREID")))
0
 

Author Comment

by:josephdaviskcrm
ID: 21771676
No, (Request("WHEREID") returns the value I'm needing.  The problem lies somewhere in the rest of the line of code.
0
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 21771749
FindByValue returns an item, not a string. use:
ddlSelectLevel.SelectedIndex = ddlSelectLevel.IndexOf(ddlSelectLevel.Items.FindByValue(Request("WHEREID")))
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21771789
Sorry you're right!
0
 
LVL 16

Expert Comment

by:ororiole
ID: 21771815
Hey even a blind squirrel like me finds a nut once in a while. :)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 48

Expert Comment

by:jpaulino
ID: 21771828
:)
0
 

Author Comment

by:josephdaviskcrm
ID: 21771917
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 21772112
Can you try ?

ddlSelectLevel.SelectedValue = ddlSelectLevel.Items.FindByValue(Request("WHEREID")).Value
0
 

Author Comment

by:josephdaviskcrm
ID: 21772135
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
 

Author Closing Comment

by:josephdaviskcrm
ID: 31466641
its ddlSelectLevel.Items.IndexOf()
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now