Keeping dropdown list state

I have a simple dropdown list which if selected, I have a selected index changes action to redirect the page back on itself with a request variable.
My problem is that once I have selected the value and the page has redirect and performed any relevant functions the  dropdown list goes back to its defaul value, How do I keep the value selected. (is this view state that I have been hearing about?). Should be a simple one as I'm sure people do it all the time.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True" Value="0">£0</asp:ListItem>
<asp:ListItem Value="20">£20</asp:ListItem>
<asp:ListItem Value="40">£40</asp:ListItem>
<asp:ListItem Value="60">£60</asp:ListItem>
<asp:ListItem Value="80">£80</asp:ListItem>
<asp:ListItem Value="100">£100</asp:ListItem>
<asp:ListItem Value="120">£120</asp:ListItem>
<asp:ListItem Value="140">£140</asp:ListItem>

===== on selected index changed ==== 
Response.Redirect(CurrentPage.SessionField = "?MinPrice=" + DropDownList1.SelectedValue )

Open in new window

Simon CrippsOwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Response.Redirect even to itself is considered a new request to the page, and so all controls will be reset.
What are you trying to accomplish?
Randy PetersonCommented:
It looks like you need to keep the state in the code behind page.  There is a Page_Load method and you need to extract the value from that and then set the SelectedIndex field to that value.  That would keep the state variable.
Simon CrippsOwnerAuthor Commented:
I would like the dropdownlist to display the value that was selected . i.e £20 , the page goes of onto itself, takes the value from the request field to set the value and it shows £20 rather than reverting back to £0.

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

If you want to display the selected value, just add a Label on the page, say Label1. then in your DDL's SelectedIndexChanged event handler, add this line instead of redirecting to the page itsefl:
Label1.Text = DropDownList1.SelectedValue
Simon CrippsOwnerAuthor Commented:
Rrooter, have you nay pointers as how I do that.
I have tried to set dropdown selectedvalue in page load, but this does not appear to work. are there any examples I can look at
Did you try my suggestion?
Simon CrippsOwnerAuthor Commented:
Codingbeaver, I still need to have the page redirect as there are several variables I populate to resubmit the page. I woould just like the refreshed page to start with a new dropdown value. Thanks for the suggestion
Then you will need to save the selected value in a Session variable before you redirect. Then in your page_Load, you retrieve the value from the Session variable and assign to the DDL so it will keep the selected state.

add this line before Response.Redirect:
Session("SelectedValue") = DropDownList1.SelectedValue

Then in your Page_Load do this:
If (Session("SelectedValue") IsNot Nothing) Then
   DropDownList1.SelectedValue = Session("SelectedValue").ToString()
End If

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Simon CrippsOwnerAuthor Commented:
That's great that works. do you know why that would work but when I use the Request Minprice for the URL redirect  (£40  it does not work.

If you can answer great, if not I will still accept the above as the correct solution cheers
It is because SelectedValue is the value of the dropdownlist item's value, which does not have a currency symbol, according to your DDL's code. You can change your DDL's value to be the same as the text and should be able to get it work.
Simon CrippsOwnerAuthor Commented:
Simon CrippsOwnerAuthor Commented:
Very helpfull thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.