Link to home
Start Free TrialLog in
Avatar of Simon Cripps
Simon CrippsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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>
 </asp:DropDownList>


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

Open in new window

Avatar of codingbeaver
codingbeaver
Flag of United States of America image

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.
Avatar of Simon Cripps

ASKER

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.

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
ASKER CERTIFIED SOLUTION
Avatar of codingbeaver
codingbeaver
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's great that works. do you know why that would work but when I use the Request Minprice for the URL redirect  (www.site1.co.uk?MinPrice=£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.
Thanks
Very helpfull thanks