Rainhider
Rainhider

Reputation: 836

Response.Redirect is set to another page, but goes through Page_Load on current page again

I have a page that has...

Page_Load(){
  if (Session["UserName"] != null){
     //code for security questions.. i have to go into this logic on every postback to check the security answer.
  }
  else{
    //sends to a page i don't want it to go to
  }
}

method(){
 ...
 Session["Username"] = null;  //if it reloads the page_load from this page, it sends it ot a page i don't want it to go to.
 Response.Redirect("nameofpage.aspx");
}

The problem is that it does not go straight to the new page. It is going through the Page_Load of this page again, and I need it to go to "nameofpage.aspx". Is there a way to send it to the next page without using Server.Transfer?

Upvotes: 0

Views: 1819

Answers (5)

Cizzan
Cizzan

Reputation: 11

If IsPostBack doesn't helps, check these values too; it should be enough to figure out what you need to do.

bool PostBack = Page.IsPostBack;
bool CrossPagePostBack = Page.IsCrossPagePostBack;
bool CallBack =  Page.IsCallback;

Upvotes: 0

Rainhider
Rainhider

Reputation: 836

Sorry guys, it was a lousy javascript _dopostback on the front side causing multiple postbacks. :(

*bangs head on desk and mutters... other ppls code

Upvotes: 0

tezzo
tezzo

Reputation: 11105

Give a look at Page.IsPostBack property to check if a the page is being rendered for the first time or is being loaded in response to a postback.

Upvotes: 1

Adam
Adam

Reputation: 2440

Your problem is that the page is doing a postback. That is why your page is firing twice I believe. In your code you should do:

if(!this.IsPostBack)
   //continue with code
else
   //something else

Also do a check on your #page directive and be sure that AutoEventWireup="true" isn't there. This typically is a root cause of the postback issue or why page_load would be called twice.

Upvotes: 0

Peter Kaplan
Peter Kaplan

Reputation: 71

The problem you're having is the redirect does a post back. You can use the function IsPostBack to check to see if it's the first time a page is loaded or if its being run on postback.

if(!Page.IsPostBack){
  // Do stuff
}

Upvotes: 0

Related Questions