Mord Fustang
Mord Fustang

Reputation: 1553

Response.redirect response is not available in context. if used with a timer.

I am trying to show a loading animation in a page for 6 seconds then redirect page.

If I use Response.Redirect(@"~\WorklistPage.aspx", true); in Page_Load it redirects page to worklistpage as expected.

But when I try to delay the redirect function, it throws an exception.

Below what I tried and not working. I get response is not available in context error

protected void Page_LoadComplete(object sender, EventArgs e)
        {
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 6000;
            aTimer.Enabled = true;

            //timer = new System.Threading.Timer(obj => { Response.Redirect(@"~\WorklistPage.aspx", true); }, null, 3000, System.Threading.Timeout.Infinite);
            //Response.Redirect(@"~\WorklistPage.aspx", true);


        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            aTimer.Enabled = false;
            //HttpContext.Current.Response.Redirect(@"~\WorklistPage.aspx");
            //Response.Redirect(@"~\WorklistPage.aspx",true);
            //Response.Redirect(@"~\WorklistPage.aspx",false);
            //Server.Transfer(@"~\WorklistPage.aspx", true);

        }

Why I am getting an error, how can I fix it?

Upvotes: 0

Views: 607

Answers (2)

Qmars
Qmars

Reputation: 31

its impossible to call a web page from ServerSide with delay u must use java script.... u must use somthing like this For Example:

protected void Page_LoadComplete(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", "alert('Submitted Successfully.'); setInterval(function(){location.href='http://www.google.com';},3000);", true);
}

Upvotes: 3

Coding Flow
Coding Flow

Reputation: 21881

Short answer is if you want to automatically redirect the browser six seconds after the page loads you need to do it in JavaScript not on the server. How do you expect a thread fired off on the server in the code behind to tell the browser to redirect to another page 6 seconds after it has returned the http response? You can do this kind of thing with signalr but it is massive overkill for a simple redirect.

Upvotes: 3

Related Questions