Solved

How to avoid page realod using Page.IsPostBack?

Posted on 2008-10-03
1
350 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now