How do I get a page to maintain control states?

T

Tom wilson

This is the same topic as the thread above, "Dealing with the Back
button". I'm getting nowhere. The closest I've come is a sample left
for me:

<%@ Page Language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
</head>
<body>
<form runat="server" ID="Form1">
<p>
&nbsp;<asp:TextBox id="TextBox1"
runat="server"></asp:TextBox>
</p>
<p>
&nbsp;<asp:Button id="Button1" runat="server"
Text="Button"></asp:Button>
</p>
</form>
</body>
</html>

1 - Load the page, enter something in the text box.
2 - Hit submit
3 - Click Back
4 - The string you entered remains.

When I put this code, in its entirety, into a new .aspx page and run
it, the textbox is blank after hitting the back button.

Why, and how can I prevent it? (before I go completely insane)

Thanks!
 
P

Patrice

I don't see what difference you make between point 1 to 4 and the later
scenario (not clear if you used submit before using the back button).

IMO :
- if something is wrong I would redisplay the whole form with the error
message so that the user doesn't even have to use the back button. Keep in
mind that you would rely here on a client side behavior that the user could
have altered (by changing its IE settings).

- for now I have the behavior you described in point 1 to 4. Could it be a
problem with your cache ? I would try to add a date/time information to the
page to see if the page with blank fields comes from the IE cache or if IE
gets it once again from the server. It could be also a IE settings. For now
it looks liike that when you using the back button, IE gets the page again
(AFAIK it was also the behavior on old IE releases, whcih version are you
using).

Good luck.

Patrice
 
T

Tom wilson

">- if something is wrong I would redisplay the whole form with the
error message so that the user doesn't even have to use the back
button."

That sounds logical, doesn't it? My main app works this way. You get
your form, fill stuff out and submit. The page returns to the browser
with red error texts above each 'errored' control. Its a work of art.
You can submit repeatedly until all errors disappear. I'm proud.

But click back anywhere during this process, even after 5 submits, the
page is returned blank. Come on, you know SOMEbody is going to click
the back button and call me up, pissed.

So I tried your time example as suggested below:

- Load form. Time is: 2:53:12
- Enter text, submit
- Time is 2:53:33
- Click "Back"
- Time is: 2:53:12

I assume the page is coming from IE's cache cause the time is the same
when going back. If it came from the server the time would be past
2:53:33, right?

So this means... asp.net pages are not capable of maintaining state?
:)

Or that IE is somehow killing the page state when it reloads the page
from its cache?

Nothing is making any sense.

(thx for the reply!)
 
C

Chad Devine

You do not have:
<%@ Page EnableViewState="True" %>
at the top of your .aspx page.

You do have:
<%@ Page Language="vb" %>

So just add on the EnableViewState like this:
<%@ Page Language="VB" EnableViewState="True" %>

That will enable your page to keep the form information.
 
J

Juan T. Llibre

Unless EnableViewState has been explicitly set to "false"
in web.config, the default setting is "true", so setting it to
"true" should not affect the page's ViewState settings.

But, maybe it *was* set to "false" in web.config... :)
 
T

Tom wilson

OMG, it works.

This tiny example works, my main app does not.

But that's the solution to my small example. You may now feel
intellectually superior. :)

for x=1 to 1000000
response.write("THANKS!!!!<BR>")
Next
 
J

Juan T. Llibre

Hmm...

It's set in machine.config ( in both v1.1 and v2.0 ).
I don't remember about v1.0, but think it was.

It's interesting to note that setting it to "true" *did* work,
so *someone* must have fiddled with that setting ... ;-)
 
T

Tom wilson

You would think. There's nothing about viewstate in web.config, I
assume that means its enabled by defualt. I have checked many times,
the properties of the form and the controls and all their viewstate
properties are set to True. But this example (single textbox) would
not work....

*WITH Internet Explorer ONLY*

Interesting, huh? Firefox and Netscape 7.1, after clicking submit and
then clicking Back, retained the value in the textbox. IE did not,
until I added that line below to enable the viewstate.

My main app does something similar but it's far more complicated; it
does a submit postback, checks for entry errors and returns the page
with error texts inserted into the pageholder sequence. It works
perfectly going forward but go back and everything is lost.

I still have to do somthing about that, I can't have users killing
page after page of data entry with a single click. Usually it's "Page
has expired", at which point you have to go "Back" until you're at the
first iteration of the page. Hmm...
 
C

Chad Devine

You're right Juan, which means this is truly a strange problem. Anyway,
glad it worked... now to figure out why it worked, ha.
 
C

Chad Devine

From what I know about viewstate, if your form is posting to another
page, all the view state information will be lost. So, in that case you
may have to use session state variables or something... Perhaps Juan
knows of something easier than throwing all form fields into session
state.
 
P

Patrice

Humm... Is it solved ?

Sounds really strange to me it was the viewstate as when the page is loaded
from the cache there is AFAIK no code that runs...

I disabled viewstate on my test page and still have the same behavior (i.e.
when the page is submitted and when I click on the back button, form fields
are left intact). I'm using IE6.

Patrice

--
 
T

Tom wilson

Actually, no. My main app works flawlessly in Netscape and Firefox.
In IE, the back button will lose all data every time. See my post
farther down. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top