Dealing with the Back button

T

Tom wilson

I can't believe this is such an impossibility...

I have an asp.net page. It accepts data through on form fields and
includes a submit button. The page loads up and you fill out some
stuff. The submit button posts the page back to the server. The
button code detects an entry error and sends the page back to the
user. This all works.

However, if the user presses the Back button at this point, we go back
and all the form values are cleared. I've been all over the web
searching for a solution and all I can find is to either disable the
back button or clear the history so Back won't go back.

I can't get either of these to work either. I've tried adding the
javascript to the top of the body of the page:

<%
Response.Buffer = True
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1)
Response.Expires = 0
Response.CacheControl = "no-cache"
%>

I've tried adding this to the Page_Load event:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Yet I can still use the back button to go back to a blank page.

What's the solution to this problem?

Thanks!
 
J

Juan T. Llibre

I suppose it wouldn't hurt to try :

<%@ OutputCache Location="None" %>

No guarantees, though.
 
T

Tom wilson

I've also tried this method:

<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">

I can use the back button all I want to return to blank pages.
 
B

Bob Barrows [MVP]

Tom said:
I can't believe this is such an impossibility...
<snip of description of by-design behavior>

I would consider putting the form values into session upon submission. Then
add code to page_load to check the session and load the values from session
into the form elements if they exist. You'll need to work out some details,
when to clear the session vars, preventing duplicate data processing, etc.

Bob Barrows
 
T

Tom wilson

The HTML source:

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="TestForm.aspx.vb" Inherits="Vista.TestForm"%>
<%@ OutputCache Location="None" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>TestForm</title>
<meta content="Microsoft Visual Studio .NET 7.1"
name="GENERATOR">
<meta content="Visual Basic .NET 7.1"
name="CODE_LANGUAGE">
<meta content="JavaScript"
name="vs_defaultClientScript">
<meta
content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<%
Response.Buffer = True
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1)
Response.Expires = 0
Response.CacheControl = "no-cache"
%>
<form id="Form1" method="post" runat="server">
<asp:button id="Button1" style="Z-INDEX: 101;
LEFT: 16px; POSITION: absolute; TOP: 80px" runat="server"

Text="Button"></asp:button><asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder></form>
</body>
</HTML>

The Page_Load:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim x As Integer
Response.Write("Test Page<BR><BR>")
For x = 1 To 10
Radios(x) = New RadioButton
Radios(x).Text = "Radio " & x
Radios(x).ID = "Radio" & x
Radios(x).GroupName = "1"
PlaceHolder1.Controls.Add(Radios(x))
Next
Response.Cache.SetCacheability(HttpCacheability.NoCache)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
End Sub

You will notice there are no less that 4 different methods for
disabling the cache yet:

1 - Load the page, click a radio
2 - Submit
3 - Back - Goes back, regardless of the above methods, to a page with
no radios selected.

Why?

Thanks...
 
T

Tom wilson

This page can have possibly hundreds of dynamically generated controls
on it, which is why I can't lose the data with a back click. I'd have
to dynamically create an unknown number of different arrays to store
the data, possibly tons of it. This is such a basic thing to do, I
don't understand why it's completely impossible without writing reams
of code to maintain the state of the pages. I thought that's what
asp.net did inherently:

http://www.w3schools.com/aspnet/aspnet_viewstate.asp

"When a form is submitted in classic ASP, all form values are cleared.
Suppose you have submitted a form with a lot of information and the
server comes back with an error. You will have to go back to the form
and correct the information. You click the back button, and what
happens.......ALL form values are CLEARED, and you will have to start
all over again! The site did not maintain your ViewState.

When a form is submitted in ASP .NET, the form reappears in the
browser window together with all form values. How come? This is
because ASP .NET maintains your ViewState. The ViewState indicates the
status of the page when submitted to the server. The status is defined
through a hidden field placed on each page with a <form
runat="server"> control. "

I don't get why the vewstate is not maintained in this example when
the above seems to imply that this is a very basic and default
behavior of asp.net pages.

Go to any site with an online application form like for a credit card.
They never have to disable the back button or post huge messages about
"Do not hit back!!!" all over the place...

Why can't I do that here?
 
J

Juan T. Llibre

Hi, Tom.

I made a very simple test page,
which you can test at http://asp.net.do/test2/test2.aspx

It only has one textbox, but should work the same no
matter how many text fields you have on the page.

Check it out, and write something in the textbox.
Then, hit your browser's back button.

The text in the texbox disappears, whcih I
believe is the behavior you want, isn't it ?

If not, please let me know the behavior you want.

Source code :
test2.aspx:
=======
<%@ Page Language="VB" %>
<%@ OutputCache Location="None" %>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</p>
</form>
</body>
</html>
============
 
T

Tom wilson

As per advice I've tried this in the Page_Load:

Response.AppendHeader("Pragma", "no-cache")
Response.AppendHeader("Cache-Control", "no-store")
Response.AppendHeader("Expires", "-1")

And this:

Response.Expires = 0
Response.Cache.SetNoStore()
Response.AppendHeader("Pragma", "no-cache")

Same result, back button still takes me Back to an unfilled page.
 
J

Juan T. Llibre

I now see you want the *opposite* of what I replied.

See
http://asp.net.do/test2/test3.aspx

That page returns the form's values when you hit the back button.
That is the *default* mode of operation for ASP.NET.

If you do *not* want values returned, then do *not*
introduce any code to force not-caching of the page.

What you want is for the page to *be* cached.

test3.aspx:
========
<%@ Page Language="VB" %>
<html>
<head>
</head>
<body>
<form runat="server">
<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>
============
 
J

Juan T. Llibre

re:
If you do *not* want values returned, then do *not*
introduce any code to force not-caching of the page.

Aargh!

That should be :

If you *do* want values returned, then do *not*
introduce any code to force no-caching of the page.

What you want is for the page to *be* cached.
 
T

Tom wilson

Thanks for the reply.

Actually, that's the opposite. I need the value to remain.

Imagine this as an online credit app. I fill out my name and for some
reason it's wrong. I go Back to correct it and the field is empty.

Now imagine this as a page with hundreds of textboxes, radios, etc.
You take 30 minutes to fill it out and submit it. The server says
there's an error and you hit Back.

Everything you did for the last 30 minutes is gone in one innocent
button click. Every online web form I've seen will maintain the data.
Why won't this?

(thx!!!)
 
T

Tom wilson

AHA!!! Yours works!

So I copied the source below, went into VS.Net and created a new
webform. I basically pasted everything below into the form.

The form loads.
I fill out the textbox and hit submit
Click Back, the textbox is empty.

Yet your link below does the opposite.

Now I'm really confused. Why would your code work on your server but
not mine?

">That is the *default* mode of operation for ASP.NET."

That's what I thought but I can't demonstrate this behavior.

(Thanks!!!)
 
I

IPGrunt

I've also tried this method:

<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">

I can use the back button all I want to return to blank pages.

Wait a minute.... you are NOT submitting the page, right?

So how could you cache it at the server?

The page lives in the Browser, not on the Server. The Server hasn't
seen the header returned by the Response message (sent from Browser
to Server when the user hits Submit).

This is a browser issue....not a server issue.

There are plugins available that help browsers maintain form values
across pages which you can recommend for your users. There may be
something you can do with IE, but I'm not much of a document object
model guy. Try the IE forum?

-- ipgrunt
 
M

MattC

remove this

Response.ExpiresAbsolute = DateTime.Now.AddDays(-1)
Response.Expires = 0
Response.CacheControl = "no-cache"

Response.AppendHeader("Pragma", "no-cache")
Response.AppendHeader("Cache-Control", "no-store")
Response.AppendHeader("Expires", "-1")

you are setting the page content to expire immediately so your browser is no
storing the content in its _local_ cache.

Also, pressing the back button does _not_ constitute a postback so there is
no notion of ViewState.

MattC
 
T

Tom wilson

As far as I know I do, there's a submit button (with no codeback code)
that is clicked to cause a postback. If the page lives in the
browser, why would the form data be cleared when the back button is
clicked ? (after the submit button is clicked)
 
T

Tom wilson

I'm trying, as one solution, to disable the back button. Since that's
not feasable, I'm attempting to clear the hitory so one can't 'go
back'. But I cna't get that to work either, out of 5 different
methods.
 
T

Tom wilson

Ok, I removed all forms of cache or history stuff.

I select a radio, click submit.
I click back and the radio is de-selected.
Also, pressing the back button does _not_ constitute a postback so there is
no notion of ViewState.

Then what does this mean?:

"When a form is submitted in classic ASP, all form values are cleared.
Suppose you have submitted a form with a lot of information and the
server comes back with an error. You will have to go back to the form
and correct the information. You click the back button, and what
happens.......ALL form values are CLEARED, and you will have to start
all over again! The site did not maintain your ViewState.

When a form is submitted in ASP .NET, the form reappears in the
browser window together with all form values. How come? ****This is
because ASP .NET maintains your ViewState. The ViewState indicates the
status of the page when submitted to the server.**** The status is
defined through a hidden field placed on each page with a <form
runat="server"> control. "

So when I click the submit button the page is sent to the server (to
do nothing really in this case). Why is the viewstate not being
maintained?

Thanks...
 
B

bruce barker

there are two senerios with the back button

1) the server did not set the page expired. when the back button is hit the
browser loads the previous page from its local disk cache with the textboxes
having the value the user entered before navigating away.

2) the server marked the page as expired. when the browser fetches the page
from the cache, it asks the server for a new copy. (you can tell if your in
this case by setting a breakpoint on the page and seeing the back button
fires it). the browser rerequests the page, it the textboxes have whatever
value your page puts in them. normally you'd store the values in a session,
and update the page if required.


-- bruce (sqlwork.com)



| I can't believe this is such an impossibility...
|
| I have an asp.net page. It accepts data through on form fields and
| includes a submit button. The page loads up and you fill out some
| stuff. The submit button posts the page back to the server. The
| button code detects an entry error and sends the page back to the
| user. This all works.
|
| However, if the user presses the Back button at this point, we go back
| and all the form values are cleared. I've been all over the web
| searching for a solution and all I can find is to either disable the
| back button or clear the history so Back won't go back.
|
| I can't get either of these to work either. I've tried adding the
| javascript to the top of the body of the page:
|
| <%
| Response.Buffer = True
| Response.ExpiresAbsolute = DateTime.Now.AddDays(-1)
| Response.Expires = 0
| Response.CacheControl = "no-cache"
| %>
|
| I've tried adding this to the Page_Load event:
|
| Response.Cache.SetCacheability(HttpCacheability.NoCache)
|
| Yet I can still use the back button to go back to a blank page.
|
| What's the solution to this problem?
|
| Thanks!
|
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top