Code executes even after Response.End()

1.2k Views Asked by At

I have to reload my page after I write some html, script to my page using Response.Write. After using Response.Write I am using Response.Flush(), followed by Response.End(). The code works properly in one server and when I deploy it to another server it is not working. As far as I know the code execution must stop once it executes the Response.End, but it is now behaving in the desired way.

Response.ClearContent();
   Response.Write("Write some html, script content here");
   Response.Flush();
   Response.End();

Using Respose.Redirect(Request.RawUrl) will not work and will throw an exception stating that the redirection cannot happen once the headers are posted...

Can some one tell me, why the code execution is not stopped by Response.End() ??

EDIT: Actual Code

protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["localTime"] == null && String.IsNullOrEmpty(Request.Params["localTime"]))
        {
            // then clear the content and write some html, a javascript code which submits the local time
            Response.ClearContent();
            Response.Write(@"<form id='local' method='post' name='local'>
                                <input type='hidden' id='localTime' name='localTime' />
                                <script type='text/javascript'>
                                    document.getElementById('localTime').value = new Date();
                                    document.getElementById('local').submit();
                                </script>
                            </form>");
            // 
            Response.Flush();

            // end the response so PageLoad, PagePreRender etc won't be executed
            Response.End();//Page must reload after this line.
        }
        else
        {

            // if the request contains the localtime, then save it in Session
            if (Request.Params["localTime"] != null)
            {
                Session["localTime"] = Request.Params["localTime"];
                Response.Write(Request.Params["localTime"] + " ~2");
                // and redirect back to the original url
                Response.Redirect(Request.RawUrl);
            }

        }

    }
0

There are 0 best solutions below