views:

449

answers:

2

I am displaying a loading div on the page init using response.write and response.flush until the time page is fully rendered . like this:

Page.Response.Write(loadinghtmlstring);
Page.Response.Flush();

I am redirecting to error page if some error occurs through try catch.

try
{}
 catch (WrapedException wre)
        {
            BaseUserControl.StoreException = wre;
            Response.Redirect(base.ErrorPagePath, false);
        }
        catch (Exception ex)
        {
            BaseUserControl.StoreException = new WrapedException(ex.Message, ex);
            Response.Redirect(base.ErrorPagePath, false);
        }

Now if any error occurs on my page after displaying loading div I got error which says :

Cannot redirect after HTTP headers have been sent.

+1  A: 

I know this may not be EXACTLY what your looking for in an answer... but why don't you use Javascript for your loader dialog? YUI has a great loading dialog here and I am sure jquery also has one as well. THis would take the complexity of having an ASP.NET generated loader on the page, and may solve your error problems.

Zoidberg
I am looking for a loader kind of control that gets displayed when i response.redirect on some page till the time page fully gets rendered.
Yeah, but with javascript, you can start the loader before the request goes out, and stop it (using javascript again) when the response gets loaded. Resulting in less clutter on the server side.
Zoidberg
+1  A: 

This is happening because you are writing and flushing the response (by flushing the headers get written). Redirect needs to overwrite these headers and cannot at this point. Once you have called Response.Flush(), there is no way to use a Response.Redirect().

You can do the following:

try
{
    Response.Write("abc");
}
catch (WrapedException wre)
{
    BaseUserControl.StoreException = wre;
    Response.Redirect(base.ErrorPagePath, false);
}
catch (Exception ex)
{
    BaseUserControl.StoreException = new WrapedException(ex.Message, ex);
    Response.Redirect(base.ErrorPagePath, false);
}
finally
{
    Response.Flush();
}
Oded
thats what i am looking for is there any way to use response.redirect() after response.flush()
Response.Flush sends the response to the browser. You can't backtrack and get the response back from the browser in order to redirect it. It is too late at this point.
Oded
cant i send response in small packets
After you send and flush, the header (that are the first thing sent) cannot be overwritten. They have already gone. `Response.Redirect` needs to write to the headers, but they are already gone. So, no you can't.
Oded