Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing selected value from Dropdown list to query.

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have a dropdown list which has a list of colleges taken from a database.

The datavalue field is set as the college id. How can I get the selected value and parse it to a query string such as

Code:
college.aspx?CollegeID={0}

Thanks
 
You just need to use the SelectedValue property of the dropdownlist:

YourDropDownListName.SelectedValue
 
I have tried that but couldnt get it working.

Do I need to put in the selectedindex changed event of the dropdownlist.

and put a response.redirect?

the dropdownlist is sitting in a ascx file. does that matter?
 
Well that is some important info you should have given in your original post.
If the ddl is within a usercontrol then you should create a public property on the usercontrol. When the ddl is changed, set that property(from the ascx code-behind). Then, from your aspx page, you can access that property.
 
Ok I have done the following to expose the value

Code:
Public ReadOnly Property DropDownSelectedValue() As String
        Get
            Return cboColleges.SelectedValue
        End Get
    End Property

And added this to test it:
Code:
Protected Sub cboColleges_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboColleges.SelectedIndexChanged
        Response.Write(DropDownSelectedValue)
    End Sub

Which works (its bringing the correct ID, but how do I use that value to load up the college.aspx page?

Thanks
 
Just access the property from your page:
YourUserConrolVariableName.DropDownSelectedValue





 
I assume I access this from the colleges.aspx page?

Firstly though I want the user to select an option and redirect them to the colleges page then bind a datalist to that value. which is where your code will come in and I will try that.

however firstly i have this code

Code:
Protected Sub cboColleges_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboColleges.SelectedIndexChanged
        Page.Response.Redirect(colleges.aspx)
    End Sub


it is coming up with an error

Code:
colleges is not declared. it may be inaccessible due to its protection level
 
You need to more clearly explain what you are trying to do.
Where is the ddl, on the page, on a usercontrol on the page?
When do you want to redirect?
Give ALL relevant information.
 
I have a usercontrol(ctlHeader) which is being used on every page as a header.

Within this usercontrol there is a dropdown list (ddlColleges). The datasource for this list is pulling a list of college names and ID from an SQL database. The ID is set to datavalue and the college name is set to the text.

This dropdownlist will have to begin with 100 colleges with probably more being added. So it wouldnt really be feasible to have 100 seperate pages (where I could use a jumpmenu). Therefore, I have a colleges.aspx which has a datalist that will display college details.

What I am trying to achieve is when the user selects a college from the dropdown list within the usercontrol from any page, this will direct them to the colleges page and populate the datalist with whatever their selection was.

This usercontrol will still be showing on the colleges.aspx page so if they select a college it will just rebind the datalist or reload the page.

Hope this makes more sense.

Thank you for your assistance
 
This code is wrong:
Code:
 Page.Response.Redirect(colleges.aspx)

Response.Redirect takes in a string. You are getting an error because it thinks colleges.aspx is an object. Look at some examples on-line.
This one is using classic asp code but the syntax is the same and what you need:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top