Solved

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

Posted on 2008-06-12
10
2,712 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
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: 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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