locked
Weird issue with Response.Redirect RRS feed

  • Question

  • User-284642143 posted

    I have a page (we'll call this Page 1) which has a large number of controls, the user enters info into. Once done they click a button to save the changes and after a successful save it redirects the user to another aspx page (Page 2).

    Currently the problem i have is when Response.Redirect is executed Page 2 displays as normal but the Page_Load event never hits (so when i have a debug point set page load never runs).

    I cleared the cache and same issue. My Response code is

    Response.Redirect(String.Format("Page2.aspx?pid={0}", ID),true);

    If i refresh the page then it hits page load.

    I have substituted true with false but same issue and cant understand why the page load event never hits but the page displays?

    Friday, March 8, 2019 4:08 PM

Answers

  • User475983607 posted

    Currently the problem i have is when Response.Redirect is executed Page 2 displays as normal but the Page_Load event never hits (so when i have a debug point set page load never runs).

    The Page Load event is part of the page life cycle and fires every time the a page is requested.

    You have a logical bug in the code most likely due to an exception handler.  The following line of code always throws a thread abort error.

    Response.Redirect(String.Format("Page2.aspx?pid={0}", ID),true);

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponse.redirect?view=netframework-4.7.2#System_Web_HttpResponse_Redirect_System_String_System_Boolean_

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2019 4:52 PM
  • User753101303 posted

    Hi,

    Does it work if you create a new blank page ? Check differerences with your Page2 and in particular if you have @Page AutoEventWriteup="true" and if your method is declared using protected void Page_Load(object sender, EventArgs e) ?

    More likely something changed that detached your event handler...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2019 6:07 PM
  • User-1174608757 posted

    Hi, EssCee

    This situation happens because Response.Redirect has a higher priority.When you  write the response.redirect in your event , for example like a button click, the program will directly run redirect without do anything else. To solve this problem, I suggest you to use window.location.herf  in stead of response.redirect. window.location.herf is a js function, you could call this function in code behind. And it could run follow by the steps of code you write.Here is the demo. I hope it could help you.

    Aspx:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            function aa() {
                window.location.href = "h1.html";
            }
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>
    

    code behind:

     public partial class redirect : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
             
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Write("run before redirect");
                ClientScript.RegisterStartupScript(this.GetType(), "", "aa()",true);
            }
        }

    You could see 

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 11, 2019 3:17 AM

All replies

  • User475983607 posted

    Currently the problem i have is when Response.Redirect is executed Page 2 displays as normal but the Page_Load event never hits (so when i have a debug point set page load never runs).

    The Page Load event is part of the page life cycle and fires every time the a page is requested.

    You have a logical bug in the code most likely due to an exception handler.  The following line of code always throws a thread abort error.

    Response.Redirect(String.Format("Page2.aspx?pid={0}", ID),true);

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponse.redirect?view=netframework-4.7.2#System_Web_HttpResponse_Redirect_System_String_System_Boolean_

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2019 4:52 PM
  • User-284642143 posted

    So to narrow this down you believe the error could be on Page1 since page 2 is displayed but the event is never hit?

    Friday, March 8, 2019 4:56 PM
  • User475983607 posted

    So to narrow this down you believe the error could be on Page1 since page 2 is displayed but the event is never hit?

    No you misunderstand.  The Page_Load event always fires because the Page_Load handler is part of the page life cycle.  As written, the redirect always causes an exception.  My best guess is you have an exception handlers that causes unexpected logical flow.  Or it can just be a bug in Page2 but we cannot see your code so we can only guess.  

    Is there anyway you can provide sample code that reproduces this issue?

    Friday, March 8, 2019 5:01 PM
  • User753101303 posted

    Hi,

    Does it work if you create a new blank page ? Check differerences with your Page2 and in particular if you have @Page AutoEventWriteup="true" and if your method is declared using protected void Page_Load(object sender, EventArgs e) ?

    More likely something changed that detached your event handler...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 8, 2019 6:07 PM
  • User-1174608757 posted

    Hi, EssCee

    This situation happens because Response.Redirect has a higher priority.When you  write the response.redirect in your event , for example like a button click, the program will directly run redirect without do anything else. To solve this problem, I suggest you to use window.location.herf  in stead of response.redirect. window.location.herf is a js function, you could call this function in code behind. And it could run follow by the steps of code you write.Here is the demo. I hope it could help you.

    Aspx:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            function aa() {
                window.location.href = "h1.html";
            }
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>
    

    code behind:

     public partial class redirect : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
             
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Write("run before redirect");
                ClientScript.RegisterStartupScript(this.GetType(), "", "aa()",true);
            }
        }

    You could see 

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 11, 2019 3:17 AM