Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropdownlist doesn't reset on page reload

Hey,...very simple question

I usually write php but happen to be programming with ASP.NET 3.0 framework now and can't figure out how to fix this.

I have a DropdownList and if I selected a some value,...my code behind does a bunch of stuff and outputs the data......

Now,...if I reload the page (ie. press F5 or the little reload thing on the browser)....all my data resets as if my selectedIndex is 0....but the actual showing Value is the previous one I selected!...i try to reset the index in my code behind in the "if ispostback = false" statement...but nothing works......it's like the Value is cached and nothing I can do changes it...

...any help would be appreciated :)

(further explanation: ....if print to screen a mydropdownlist.selectedIndex from my Page load Sub...It will return a 0....however the selected index on the screen is clearly not the 0 one....I understand WHY this is happening...i just need to stop it....or at least have a way of determining the index in the view state...)

Andrew

like image 327
Andrew Avatar asked Mar 22 '09 02:03

Andrew


3 Answers

This is normal behaviour for form fields. Browsers generally try to remember the contents of text fields, the states of checkboxes and the selected items in dropdowns over events like page reload and back/forwards.

For this reason you should not assume during script initialisation that the values of your form fields will match the content you served up in the HTML. Have the script sniff their current values and set its variables and DOM state up accordingly when the page loads.

If you really want to throw away all user form field changes on reload/navigation, call form.reset() in the script initialisation to return it to the HTML state. But this can be quite user-unfriendly in normal circumstances.

like image 125
bobince Avatar answered Nov 10 '22 06:11

bobince


It sounds like when you hit f5 you are NOT doing a new page reload. You say that when you select something from your dropdownlist that you do a bunch code behind stuff. Well that action IS a postback. So hitting f5 AFTER that action would not be considered a new call to the page so the the if (IsPostBack == false) would fail because it is a postback.

Your first action caused it to be a postback so hitting f5 after that, will be a postback.

It sounds like you may have some viewstate issues as well since your other controls are resetting. Although if you are doing the hide / shows with client side scripting, they will not be persisted through the viewstate anyways.

like image 24
Kelsey Avatar answered Nov 10 '22 06:11

Kelsey


Try disabling veiwstate on the checkbox if you don't need it.

like image 1
OJ. Avatar answered Nov 10 '22 06:11

OJ.