Solved

How to avoid page realod using Page.IsPostBack?

Posted on 2008-10-03
1
381 Views
Last Modified: 2012-05-05
I have a confirmation page that when it loads up the first time it executes some code. What I'm trying to prevent is when you refresh that page it should not execute the code anymore and hence should do something else e.g. redirect to another page.

The code is as follows:

If Page.IsPostBack = False Then

//code to be executed when loaded the first time

Else

//code to redirect to another page

End If

The problem I'm having is when you initially get to the page (for the first time) it executes the code in the if condition, but when I refresh the page it again executes the code in the if statement, where as it should execute the code in the else statement.

Can anyone see where I'm going wrong?

Thanks
0
Comment
Question by:Gary2Seven
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 22632543
the "postback" is ONLY true when you have a postback, ie via a button.
if you use F5 (in IE) to refresh the page, it will not be a postback.

to solve that, you have to set a session variable, for example, on the IsPostBack = false, and check on that one.
If Page.IsPostBack = False Then
  If Session("page_loaded") = "YES" Then
    //code to redirect to another page
    // -> in that other page, the session "page_loaded" needs to be cleared again!!
  Else
    //code to be executed when loaded the first time
   
    Session("page_loaded") = "YES"
  End If
Else
 //code to redirect to another page
End If

Open in new window

0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question