Note that there are some explanatory texts on larger screens.

plurals
  1. POIn C# asp.net is it possible to send back a valid web page half way through a method?
    primarykey
    data
    text
    <p>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.</p> <pre><code>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"); } } </code></pre> <p>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.</p> <p>Is this possible?</p> <p>I thought Response.Flush(); Response.End(); might do the trick - but the webpage sent back to the browser causes XML Parse errors.</p> <p>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.</p> <p>Before the new method I used this code e.g.:</p> <pre><code>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 </code></pre> <p>Now it's invoked like this:</p> <pre><code>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 </code></pre> <p>so I could obviously use</p> <p>else { ...rest of processing }</p> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload