简体   繁体   English

Response.Redirect设置为另一个页面,但再次通过当前页面的Page_Load

[英]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". 它再次经过此页面的Page_Load,我需要它转到“ nameofpage.aspx”。 Is there a way to send it to the next page without using Server.Transfer? 有没有一种方法可以在不使用Server.Transfer的情况下将其发送到下一页?

查看Page.IsPostBack属性,以检查页面是第一次呈现还是响应回发而加载。

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. 您可以使用IsPostBack函数检查页面是第一次加载还是在回发时运行。

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

Your problem is that the page is doing a postback . 您的问题是该页面正在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. 还要检查您的#page指令,并确保AutoEventWireup="true"不存在。 This typically is a root cause of the postback issue or why page_load would be called twice. 这通常是回发问题的根本原因,或者为什么page_load将被调用两次。

Sorry guys, it was a lousy javascript _dopostback on the front side causing multiple postbacks. 抱歉,这是前端的糟糕javascript _dopostback,导致多次回发。 :( :(

*bangs head on desk and mutters... other ppls code *刘海在桌子上杂乱无章 ... 其他ppls代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM