Solved

How to avoid page realod using Page.IsPostBack?

Posted on 2008-10-03
1
369 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
1 Comment
 
LVL 142

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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