Solved

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

Posted on 2008-06-12
10
2,709 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

17 Experts available now in Live!

Get 1:1 Help Now