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.