473,765 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple page won't maintain state

I have created a very simple example that doesn't work. Form1
contains a textbox and a button:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Response.Redire ct("login.aspx" )
End Sub

Very simple. Forget what login.aspx does, it's just somewhere to
redirect to.

So, I load this page, enter text and click the button. It takes me to
login.aspx. Fine.

Hit the back button and the textbox is empty. Why is the textbox
empty? Aren't .aspx pages supposed to maintain their state?

Both the textbox and the form have their viewstate properties set to
true.

Thanks!

Nov 19 '05 #1
7 2041
Tom:
They maintain their state through server-side round trips. That is, when
you click a linkbutton or a button the page will postback to itself and
state will be preserved. In your case, when you click Button1 the page
posts back to itself, and if you were to check the textbox text property,
you would see the value is maintained. When you Redirect you are going to
another page, at this point your persistante state is lost...the viewstate
is no longer kept. When you click back, you are going back to the original
page, state isn't maintained (it's already been thrown away)...it's like ur
visiting it for the first time. In other words, viewstate persists data
whilst doing roundtrips to the same page. The minute you Response.Redire ct,
the viewstate is lost. hitting back is like requesting Form1 for the very
first time.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Tom wilson" <ye*******@nosp am.com> wrote in message
news:55******** *************** *********@4ax.c om...
I have created a very simple example that doesn't work. Form1
contains a textbox and a button:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Response.Redire ct("login.aspx" )
End Sub

Very simple. Forget what login.aspx does, it's just somewhere to
redirect to.

So, I load this page, enter text and click the button. It takes me to
login.aspx. Fine.

Hit the back button and the textbox is empty. Why is the textbox
empty? Aren't .aspx pages supposed to maintain their state?

Both the textbox and the form have their viewstate properties set to
true.

Thanks!

Nov 19 '05 #2
Tom:
They maintain their state through server-side round trips. That is, when
you click a linkbutton or a button the page will postback to itself and
state will be preserved. In your case, when you click Button1 the page
posts back to itself, and if you were to check the textbox text property,
you would see the value is maintained. When you Redirect you are going to
another page, at this point your persistante state is lost...the viewstate
is no longer kept. When you click back, you are going back to the original
page, state isn't maintained (it's already been thrown away)...it's like ur
visiting it for the first time. In other words, viewstate persists data
whilst doing roundtrips to the same page. The minute you Response.Redire ct,
the viewstate is lost. hitting back is like requesting Form1 for the very
first time.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Tom wilson" <ye*******@nosp am.com> wrote in message
news:55******** *************** *********@4ax.c om...
I have created a very simple example that doesn't work. Form1
contains a textbox and a button:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Response.Redire ct("login.aspx" )
End Sub

Very simple. Forget what login.aspx does, it's just somewhere to
redirect to.

So, I load this page, enter text and click the button. It takes me to
login.aspx. Fine.

Hit the back button and the textbox is empty. Why is the textbox
empty? Aren't .aspx pages supposed to maintain their state?

Both the textbox and the form have their viewstate properties set to
true.

Thanks!

Nov 19 '05 #3

Ok, I understand that.

Lets start over, that was an inaccurate example.

This is a survey page that works like so:

- User gets page, fills out, submits
- Submit codeback sees entry errors and inserts error messages as
LiteralControls into the Placeholder sequence.
- The user gets the page back with the errors inline. All is ok.

At this point there are 2 choices:

1 - The user corrects the error and re-submits. That works.
or
2 - The user hits the back button to correct the error. In this case,
after clicking back, all the controls on the page are reset.

So there is no response.redire ct at all here. If one moves forward
(correct, submit, correct, submit) it works perfect. If the back
button is clicked, the form reverts to its previous state (error
messages still in place) but the controls are all reset.

How does one deal with maintaining state when the user hits the Back
button?

On Mon, 14 Feb 2005 15:39:43 -0500, "Karl Seguin" <karl REMOVE @
REMOVE openmymind REMOVEMETOO . ANDME net> wrote:
Tom:
They maintain their state through server-side round trips. That is, when
you click a linkbutton or a button the page will postback to itself and
state will be preserved. In your case, when you click Button1 the page
posts back to itself, and if you were to check the textbox text property,
you would see the value is maintained. When you Redirect you are going to
another page, at this point your persistante state is lost...the viewstate
is no longer kept. When you click back, you are going back to the original
page, state isn't maintained (it's already been thrown away)...it's like ur
visiting it for the first time. In other words, viewstate persists data
whilst doing roundtrips to the same page. The minute you Response.Redire ct,
the viewstate is lost. hitting back is like requesting Form1 for the very
first time.

Karl


Nov 19 '05 #4

Ok, I understand that.

Lets start over, that was an inaccurate example.

This is a survey page that works like so:

- User gets page, fills out, submits
- Submit codeback sees entry errors and inserts error messages as
LiteralControls into the Placeholder sequence.
- The user gets the page back with the errors inline. All is ok.

At this point there are 2 choices:

1 - The user corrects the error and re-submits. That works.
or
2 - The user hits the back button to correct the error. In this case,
after clicking back, all the controls on the page are reset.

So there is no response.redire ct at all here. If one moves forward
(correct, submit, correct, submit) it works perfect. If the back
button is clicked, the form reverts to its previous state (error
messages still in place) but the controls are all reset.

How does one deal with maintaining state when the user hits the Back
button?

On Mon, 14 Feb 2005 15:39:43 -0500, "Karl Seguin" <karl REMOVE @
REMOVE openmymind REMOVEMETOO . ANDME net> wrote:
Tom:
They maintain their state through server-side round trips. That is, when
you click a linkbutton or a button the page will postback to itself and
state will be preserved. In your case, when you click Button1 the page
posts back to itself, and if you were to check the textbox text property,
you would see the value is maintained. When you Redirect you are going to
another page, at this point your persistante state is lost...the viewstate
is no longer kept. When you click back, you are going back to the original
page, state isn't maintained (it's already been thrown away)...it's like ur
visiting it for the first time. In other words, viewstate persists data
whilst doing roundtrips to the same page. The minute you Response.Redire ct,
the viewstate is lost. hitting back is like requesting Form1 for the very
first time.

Karl


Nov 19 '05 #5
Tom:
it seems to me that choice #1 and #2 are the same thing (or atleast that the
user would be doing them for the same purpose). Not sure why users would hit
the back button to correct if it's clear that they can simply correct it on
the page infront ofthem and hit resumit (#1).

You can do a google search for 'ASP.net back button' and get a lot of
relevant hits. Remeber:

User views page.aspx
User submits and postback to page.aspx
from this point, when the user hits "back" it's like he/she is back at step
one, viewing page.aspx for the first time.

Not much you can do about it...but in my experience users know this is how
web apps work...fill out a form, get an error message and make
correction....t hey know not to hit back when the error message is staring
them in the face... Perhaps what you should do in addition to the error
message is display a red asterix next to the field in error so that their
attention is drawn there for correction instead of to the back button...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Tom wilson" <ye*******@nosp am.com> wrote in message
news:25******** *************** *********@4ax.c om...

Ok, I understand that.

Lets start over, that was an inaccurate example.

This is a survey page that works like so:

- User gets page, fills out, submits
- Submit codeback sees entry errors and inserts error messages as
LiteralControls into the Placeholder sequence.
- The user gets the page back with the errors inline. All is ok.

At this point there are 2 choices:

1 - The user corrects the error and re-submits. That works.
or
2 - The user hits the back button to correct the error. In this case,
after clicking back, all the controls on the page are reset.

So there is no response.redire ct at all here. If one moves forward
(correct, submit, correct, submit) it works perfect. If the back
button is clicked, the form reverts to its previous state (error
messages still in place) but the controls are all reset.

How does one deal with maintaining state when the user hits the Back
button?

On Mon, 14 Feb 2005 15:39:43 -0500, "Karl Seguin" <karl REMOVE @
REMOVE openmymind REMOVEMETOO . ANDME net> wrote:
Tom:
They maintain their state through server-side round trips. That is, when
you click a linkbutton or a button the page will postback to itself and
state will be preserved. In your case, when you click Button1 the page
posts back to itself, and if you were to check the textbox text property,
you would see the value is maintained. When you Redirect you are going toanother page, at this point your persistante state is lost...the viewstateis no longer kept. When you click back, you are going back to the originalpage, state isn't maintained (it's already been thrown away)...it's like urvisiting it for the first time. In other words, viewstate persists data
whilst doing roundtrips to the same page. The minute you Response.Redire ct,the viewstate is lost. hitting back is like requesting Form1 for the very
first time.

Karl

Nov 19 '05 #6
It seems like a rather obvious hole in the technology. You know
somebody is going to click the back button and lose pages and pages of
data. I remember another developer I knew would not write delete
confirmations into his apps. When asked 'what if someone hits delete
by accident' his reply was 'who's going to do that'? A lot of people.

This page leads me to believe this can be done:

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.......A LL 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 according to this, this is a standard feature of asp.net yet I
can't get it to work. It seems a lot of people in this group ask
about disabling the back button but it's never a recommended practice.
I already do display error messages above the question in red, but
somebody's going to hit Back and freak out. They would tell me, as I
would say, 'I have yet to see an online form that says "DO NOT hit the
back button!!!!"'

I'll search around the web but I figured these being the Microsoft
newsgroups, that an answer to this would be simple and routine. Does
no one write apps capable of dealing with the back button and loss of
data?

Thanks for the replies...

On Tue, 15 Feb 2005 08:42:31 -0500, "Karl Seguin" <karl REMOVE @
REMOVE openmymind REMOVEMETOO . ANDME net> wrote:
Tom:
it seems to me that choice #1 and #2 are the same thing (or atleast that the
user would be doing them for the same purpose). Not sure why users would hit
the back button to correct if it's clear that they can simply correct it on
the page infront ofthem and hit resumit (#1).

You can do a google search for 'ASP.net back button' and get a lot of
relevant hits. Remeber:

User views page.aspx
User submits and postback to page.aspx
from this point, when the user hits "back" it's like he/she is back at step
one, viewing page.aspx for the first time.

Not much you can do about it...but in my experience users know this is how
web apps work...fill out a form, get an error message and make
correction.... they know not to hit back when the error message is staring
them in the face... Perhaps what you should do in addition to the error
message is display a red asterix next to the field in error so that their
attention is drawn there for correction instead of to the back button...

Karl


Nov 19 '05 #7
Also in classic forms, if you hit the back button, the form fields will
still be with the data. (try!)
The viewstate gives you more options.
If in any case you want the form to be filled with the details, consider
using the session object to maitain data for each user. If you don't want
this, then add the ifpostback=fals e and reset all fields.

Leo

"Tom wilson" <ye*******@nosp am.com> wrote in message
news:rf******** *************** *********@4ax.c om...
It seems like a rather obvious hole in the technology. You know
somebody is going to click the back button and lose pages and pages of
data. I remember another developer I knew would not write delete
confirmations into his apps. When asked 'what if someone hits delete
by accident' his reply was 'who's going to do that'? A lot of people.

This page leads me to believe this can be done:

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.......A LL 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 according to this, this is a standard feature of asp.net yet I
can't get it to work. It seems a lot of people in this group ask
about disabling the back button but it's never a recommended practice.
I already do display error messages above the question in red, but
somebody's going to hit Back and freak out. They would tell me, as I
would say, 'I have yet to see an online form that says "DO NOT hit the
back button!!!!"'

I'll search around the web but I figured these being the Microsoft
newsgroups, that an answer to this would be simple and routine. Does
no one write apps capable of dealing with the back button and loss of
data?

Thanks for the replies...

On Tue, 15 Feb 2005 08:42:31 -0500, "Karl Seguin" <karl REMOVE @
REMOVE openmymind REMOVEMETOO . ANDME net> wrote:
Tom:
it seems to me that choice #1 and #2 are the same thing (or atleast that
the
user would be doing them for the same purpose). Not sure why users would
hit
the back button to correct if it's clear that they can simply correct it
on
the page infront ofthem and hit resumit (#1).

You can do a google search for 'ASP.net back button' and get a lot of
relevant hits. Remeber:

User views page.aspx
User submits and postback to page.aspx
from this point, when the user hits "back" it's like he/she is back at
step
one, viewing page.aspx for the first time.

Not much you can do about it...but in my experience users know this is how
web apps work...fill out a form, get an error message and make
correction... .they know not to hit back when the error message is staring
them in the face... Perhaps what you should do in addition to the error
message is display a red asterix next to the field in error so that their
attention is drawn there for correction instead of to the back button...

Karl

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2921
by: Fred | last post by:
I have an aspx page: Main.aspx which has several components that cause a postback. In the page Main.aspx I also have an iframe which contains a multi-page pdf file. The problem I am facing is that whenever the user does an action which causes a postback on the main page Main.aspx, the page posts back and the pdf file in the iframe is reloaded. This is a problem becuase if the user has rotated or magnified the page then that is lost. ...
6
2481
by: Jeff Smythe | last post by:
Why does Session_Start in Global.asax fire for every page opened during a session of an ASP.NET application? Am I wrong to expect that it would fire only when the first page (i.e., any page in the app that is opened before any other page during the session) is opened? Thanks.
5
2631
by: Stephanie_Stowe | last post by:
Hi. I am trying to get used to AS.NET. I have been doing ASP classic for years, and am now in a position to do ASP.NET. I am in the stumbling around until I get my bearings phase. I hope you will bear with me. I am going through the QuickStart. After reading a little, I am trying to implement a simple page on a simple project I have made up. I have a page called default.aspx. I want it to load a list of user names from a SQL database...
2
1071
by: Ralph Krausse | last post by:
My source to my test is below. I am trying to figure out state. I create a int and a Test object and set them to some value on Page_Load. When I click my button on my web page, Button1_Click gets called but a = 0 and objTest.Name is null. Now it does make sense than when the button is clicked, that a should be 0 and objTest should be null but I thought that ASP.NET took care of this. Do I have to go back to old ASP days and set this stuff...
0
317
by: Tom wilson | last post by:
I have created a very simple example that doesn't work. Form1 contains a textbox and a button: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Response.Redirect("login.aspx") End Sub Very simple. Forget what login.aspx does, it's just somewhere to redirect to.
10
1445
by: Carlos | last post by:
Hi all, I am currently working on a project that needs to maintain the connection to the DB throughout the pages that the user navigates. I have already validated my DB connection and some operations on the start-up page, but I am not sure how to make the same persist in the code behind for the other pages using C#. I appreciate any help. Thanks,
7
2110
by: Shadow Lynx | last post by:
I realize that his question has been asked, in many other forms, many times in this group. Even so, my tired eyes have not yet found a sufficient answer, so I've decided to "reask" it even though I'm sure this will offend some of the more seasoned verterans of this board. The Players: Consider a simple web form, which I'll call Page A, that has a couple of controls on it. It's not important what they are, only that they're static...
4
1354
by: Raj | last post by:
Hi, I am struggling with this for quite some time and thought will seek your help on it. What I am doing is, I have Page1.aspx where user inputs text into text boxes and also has a link to Page2.aspx. On Page2.aspx user will input some more info and has two buttons 'Save' and 'Cancel'. Both the buttons on Page2.aspx should take the user back to Page1.aspx and the data entered by the user on Page1 should not be lost. So,...
5
4080
by: =?Utf-8?B?U2FjaGluIFNha2k=?= | last post by:
I have asp application, from which I am redirecting user to .aspx page. I want to use same sessions in .aspx application. When User clicks on Browsers back button i.e. comes back from .aspx page to .asp page the same session state i want to persist ? How to do this ?? Thanks in Advance Sachin Saki
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7371
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.