• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 587
  • Last Modified:

DropDownList item selected from QueryString

I am populating a dropdown list from a database with the following code
I am passing a suspectid to the page via a query string.  I would like to have the dropdown list item that matches the querystringvalue to be automatically selected when the page is loaded. This was easy enough to do in classic ASP. I am wondering how I can do this with .NET.  Any suggestions?

I want the entire list to show from the db but I do want the specific item selected.



Page Load subroutine --
objCommand = New ODBCCommand("SELECT suspectid, lastname FROM suspects ", objConnection)
    objReader = objCommand.ExecuteReader()  
    suspectid.DataSource = objReader
    suspectid.DataValueField = "suspectid"
    suspectid.DataTextField = "lastname"
    suspectid.DataBind()
    objReader.Close()

Form Tag/Control ---
<asp:DropDownList id="suspectid"  runat="server" />
0
baybook
Asked:
baybook
  • 3
  • 3
1 Solution
 
Bob LearnedCommented:
What kind of problems are you having?

Bob
0
 
Bob LearnedCommented:
You haven't specified any WHERE clause, using the query string:

objCommand = New ODBCCommand(String.Format("SELECT suspectid, lastname FROM suspects WHERE suspectid = {0}", Request.QueryString("id"), objConnection)
    objReader = objCommand.ExecuteReader()  
    suspectid.DataSource = objReader
    suspectid.DataValueField = "suspectid"
    suspectid.DataTextField = "lastname"
    suspectid.DataBind()
    objReader.Close()

Bob
0
 
baybookAuthor Commented:
The query to pull the drop down list is good.  It populates with all the possible suspects.
In classic ASP  I can mar one of these items as selected  based on the list value.

Is there a way I can mark one of these as selected based on some value?

The reason I ask is that I want to use this page when suspectid is passed or not passed.  I also need the ability to change to on of the other suspectids other than the one that MIGHT be passed in the query string.
0
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!

 
Bob LearnedCommented:
   suspectid.SelectedIndex = suspectid.Items.IndexOf(suspectid.Items.FindByText(Text))

Bob
0
 
baybookAuthor Commented:
Should I put the suspectid into the inner TEXT  area?  ie:  This is what I am trying
suspectid.SelectedIndex = suspectid.Items.IndexOf(suspectid.Items.FindByText( request.querystring("suspectid") ))
0
 
baybookAuthor Commented:
I'm trying
suspectid.SelectedIndex = suspectid.Items.IndexOf(suspectid.Items.FindByValue( 47658999 ))

suspectid.SelectedIndex = suspectid.Items.IndexOf(suspectid.Items.FindByValue( request.querystring("suspectid") ))

But my list item is not selected
0
 
skiltzCommented:
suspectid.SelectedValue = request.querystring("suspectid")
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now