Keeping dropdown list state

Simon Cripps
Simon Cripps used Ask the Experts™
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

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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?
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.


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.

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

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


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?


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


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.




Very helpfull thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial