Solved

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

Posted on 2008-06-12
10
2,717 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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