• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

CustomValidator doesn't remember it's state after browser back button

Hello,

I have an aspx page. On this page the user can make a few selections. When I hit the 'Continue' button I am validating his selections (database access). When the validation fails I set (server-side) : myValidator.IsValid = false;
The page reloads and the validator shows the error message. So far so good.

When I correct the mistake and hit the 'Continue' button, a second page loads. But before the page loads, I set : myValidator.IsValid = true;
I -SEE- the validator disappear from the page before the second page loads, which is ok. But when I click on the back button of the browser (after the second page has loaded), the validator is visible again! Even though I did IsValid = true

It looks like the application is ignoring the IsValid = true. How can I make sure the validator remains invisible?
0
JasperB
Asked:
JasperB
  • 2
1 Solution
 
shovavnikCommented:
Initilalize it to IsValid = true in your Page_Load event.  You may have to set the cache expiration to force the page to load from the server in addition.

I'm guessing that when you hit back on your browser, it reloads the previous state of the page before it was posted.

That is, it looks like the following is happening (I could be wrong):

1. User clicks Continue.
2. PostBack - IsValid = false.
3. Page is sent back to the client.
4. User clicks Continue.
5. PostBack - IsValid = true.
6. Redirection (either by Execute or Redirect).
7. New Page is sent to the client.
8. User hits back.
9. The browser is only familiar with the page before it was validated, so it shows the invalid page it saw in step 3.
0
 
JasperBAuthor Commented:
I think you are right with the 9 steps you mentioned. Question is: Why doesn't it remember the state in step 5?

I tried to set the expiration to 0 and no caching but when I press the back button of the browser I get the message that the page is expired (but I want to load it automatically) and when I refresh the validator is also visible again.

So the best would be to remember the state of the page.
It that's not possible I could reload the page after the back button. And in that case: How can I make it reload automatically, instead of displaying that I have to refresh manually?
0
 
shovavnikCommented:
It doesn't remember step 5 because step 5 never gets to the client.

That is, step occurrs on the server and because the page is validated, step 6 redirects to another page.  Regardless of the type of redirection, as long as its server-side redirection, the browser never gets an opportunity to cache the results of the validated page because it's never sent to the browser.

You have several options (not in any particular order):
1. Do the redirection on the client.  That is, do not redirect on the server-side.  Use a javascript function to redirect on the document's onLoad() event.
2. Don't support the browser's back button.  This is probably not feasible for an internet application, but for an intranet application it might be a good solution.  Instead of a back button, provide your own navigation buttons which will redirect to your page to give the user a way to go "Back".
3. Store the state in the session.  This is a bit problematic, because you usually don't want to load the session state if the page has not posted back, which is what you want to do here, so you'll have to be careful about the logic flow.
4. Use the Cache object.  I haven't used it much or read up on it yet, but it should be able to do what you want.  If I'm not mistaken, you'd save the page validation state to the cache the same way you would to the session or the viewstate, except that you can set expiration for the specific cache.  Lookup on this page Caching page output and also caching portions of a page.
http://support.microsoft.com/default.aspx?scid=kb;en-us;307225
5. If you can merge the two pages (reasonably) then you can avoid this problem to begin with.

If I think of other options, I'll post.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now