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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp:dropdownlist problem

Status
Not open for further replies.

dhmfh

Programmer
Nov 28, 2005
69
GB
I have the following code. When I select from the drop down and click sumbit i need the drop down to hold the value I selected when I go to another page. What am I missing from the below?

Protected Sub SelLocations_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles SelLocations.Load

'This Will DataBind The Dates Drop Down Control
If Not Me.Page.IsPostBack Then

SelLocations.DataSource = Search.Locations
SelLocations.AutoPostBack = False
SelLocations.DataValueField = "Location_Id"
SelLocations.DataTextField = "Location_Name"
SelLocations.DataBind()

Else
SelLocations.SelectedValue = Me.Session("Locations")

End If

End Sub
 
Use the Items.FindByValue method of the DropDownList otherwise you are just changing the value rather than setting the selected item.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm,

I have added this now and still no luck. Any ideas?

Protected Sub SelLocations_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles SelLocations.Load

'This Will DataBind The Dates Drop Down Control
If Not Me.Page.IsPostBack Then

SelLocations.DataSource = Search.Locations
SelLocations.AutoPostBack = False
SelLocations.DataValueField = "Location_Id"
SelLocations.DataTextField = "Location_Name"
SelLocations.DataBind()

Else

SelLocations.SelectedIndex = SelLocations.Items.FindByValue(Me.Session("Locations")).Value

End If

End Sub



 
You are setting the Selected{b]Index[/b] to the Value that you have found. Use the index of the found item instead.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm,

I am now returning the correct value, but still unable to ensure the drop down stays on this value. I am having one of those days and cannot see where I am going wrong

Protected Sub SelLocations_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles SelLocations.Load

'This Will DataBind The Dates Drop Down Control
If Not Me.Page.IsPostBack Then

SelLocations.DataSource = Search.Locations
SelLocations.AutoPostBack = True
SelLocations.DataValueField = "Location_Id"
SelLocations.DataTextField = "Location_Name"
SelLocations.DataBind()

Else

Response.Write(SelLocations.Items.FindByValue(SelLocations.SelectedValue).Value)
SelLocations.SelectedValue = (SelLocations.Items.FindByValue(SelLocations.SelectedValue).Value)

End If

End Sub
 
Try this:
Code:
SelLocations.Items.FindByValue(Session("Locations")).Selected = True



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Tried that and got a Object reference not set to an instance of an object error. No Joy!

Protected Sub SelLocations_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles SelLocations.Load

'This Will DataBind The Dates Drop Down Control
If Not Me.Page.IsPostBack Then

SelLocations.DataSource = Search.Locations
SelLocations.AutoPostBack = True
SelLocations.DataValueField = "Location_Id"
SelLocations.DataTextField = "Location_Name"
SelLocations.DataBind()

Else

Response.Write(SelLocations.Items.FindByValue(SelLocations.SelectedValue).Value)
'SelLocations.SelectedValue = (SelLocations.Items.FindByValue(SelLocations.SelectedValue).Value)
SelLocations.SelectedValue = SelLocations.Items.FindByValue(Session("Locations")).Selected = True

End If

End Sub
 
Use my example by itself...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Tried that and got a Object reference not set to an instance of an object error.

Protected Sub SelLocations_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles SelLocations.Load

'This Will DataBind The Dates Drop Down Control
If Not Me.Page.IsPostBack Then

SelLocations.DataSource = Search.Locations
SelLocations.AutoPostBack = False
SelLocations.DataValueField = "Location_Id"
SelLocations.DataTextField = "Location_Name"
SelLocations.DataBind()

Else

SelLocations.Items.FindByValue(Session("Locations")).Selected = True

End If

End Sub
 
It can't find an item with that value then...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
hold the value I selected when I go to another page.

meaning you leave the page, but when u return to that page you want the value previously selected to be there?

if so, returning the page would not be a post back, so the first part would be run only...

i think your else postback is your problem

Code:
    Protected Sub SelLocations_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles SelLocations.Load

        'This Will DataBind The Dates Drop Down Control
        If Not Me.Page.IsPostBack Then
            	SelLocations.DataSource = Search.Locations
            	SelLocations.AutoPostBack = False
           	SelLocations.DataValueField = "Location_Id"
            	SelLocations.DataTextField = "Location_Name"
            	SelLocations.DataBind()
        End If

        'You'll still need some null error handling when you use session state

	   If Not Session("Locations") = "" Then		
		SelLocations.Items.FindByValue(Session("Locations")).Selected = True
 	   End If

    End Sub

 
Thanks adamroof that worked. The only problem I have now is that when I have set the session variables through the first time it is ran, if i change a selection in the drop down to a new value and run the code below its not picking up the new value of drop down and putting it into the session variable

Protected Sub BasicSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BasicSearch.Click

Session("StartDate") = SelDates.SelectedValue
Session("Weeks") = SelWeeks.SelectedValue
Session("Beds") = SelBedroomNo.SelectedValue
Session("Locations") = SelLocations.SelectedValue

Response.Redirect("/MFH/PropertySearch.aspx")

End Sub
 
Code:
Protected Sub BasicSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BasicSearch.Click

        Session("StartDate") = SelDates.SelectedValue
        Session("Weeks") = SelWeeks.SelectedValue
        Session("Beds") = SelBedroomNo.SelectedValue
        Session("Locations") = SelLocations.SelectedValue
[b]
        'this doesnt carry session info i think
        'Response.Redirect("/MFH/PropertySearch.aspx")

        'this would i think
        Server.Transfer("/MFH/PropertySearch.aspx")
[/b]
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top