views:

45

answers:

1

When I want to redirect a user who has lost session state back to the StartPage I find Response.Redirect("~/StartPage.aspx") causes the error "Response Redirect Cannot Be Called In A Page Callback". Therefore in a Page.IsCallback situation I want to add javascript to do the redirect - this works - see code below.

protected void ForceBackToStartPage()
{
    if (Page.IsCallback)
    {
        string fullLoginUrl = ConvertRelativeUrlToAbsoluteUrl( "~/StartPage.aspx" );
        string script = string.Format("window.location.href = '{0}';", fullLoginUrl);
        ClientScriptManager cm = Page.ClientScript;
        cm.RegisterClientScriptBlock(GetType(), "redirect", script, true);
        //Response.Flush();
        //Response.End();
    }
    else
    {
        Response.Redirect("~/StartPage.aspx");
    }

}

After I have added the javascript in this section the code continues to process normally leaving this function and going off to other processing sections of the webpage - probematic as the user has no session data. Ideally I'd want processing of this web page to finish - and for the page to return in a valid state so that the user gets redirected.

Is this possible?

I thought Response.Flush(); Response.End(); might do the trick - but the webpage sent back to the browser causes XML Parse errors.

Further info: I check for missing session data in a few places (depending on the webpage - this code is in lots of web pages) - for instance - PageLoad and from some submit button methods.

Before the new method I used this code e.g.:

protected void Page_Load(object sender, EventArgs e)
{
    if ((string)Session["OK"] != "OK")
    {
        // the session has timed out - take them back to the start page
        Response.Redirect("~/StartPage.aspx");
    }

...rest of processing

Now it's invoked like this:

protected void Page_Load(object sender, EventArgs e)
{
    if ((string)Session["OK"] != "OK")
    {
        // the session has timed out - take them back to the start page
        ForceBackToStartPage();
    }

...rest of processing

so I could obviously use

else { ...rest of processing }

but I was hoping not to have to do that as SVN Source Control takes that to mean I've re-written the whole 'rest of processing' section. This is problematic as when I merge from a branch into the trunk it does not check I am not overwriting mods because it assumes it's all new code coming in.

+1  A: 

Response.Redirect issues a "302 Moved" command that cannot be served asynchronously. Given that you're just registering a script when IsCallback=true, could you not also do it when IsCallback= false? Register a script that hooks up an event to the loaded event of the window and redirect the page from script instead of from the server.

Dave Swersky
It's the registering of the script when IsCallback=true thats causing me problems (the code for IsCallback=false works as I don't get the Response.Redirect errors any more) as I want to be able to break off processing and send a valid page back at that point. When processing exits "ForceBackToStartPage" it then goes on and does the normal processing for the page - depending on which web page is involved it hits an error due to no session data being available... which looks like a bug in the Event Log - when ideally that piece of code should not have executed anyway.
Paul H