Response.Redirect in a page callback

Today, while I was testing my aspx page.. my session expired & in Page_Load event, I got this error “Response.Redirect cannot be called in a Page callback.”.

I checked for this error in google & there were many with javascript as a solution. Here it goes.

string script = string.Format("document.location.href = '{0}');", "~/logon.aspx");
ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "redirect", script, true);

But this was not HELPFUL in my case. Then, I found this & yes. this WORKS.

Response.RedirectLocation = "logon.aspx";

Since I use Devexpress controls, I could also use

ASPxWebControl.RedirectOnCallback("logon.aspx");

My working solution:

if (Session["LoggedInUser"] == null)
            {
                Session.Abandon();
                if (IsCallback)
                {
                    ASPxWebControl.RedirectOnCallback("logon.aspx");
                }
                else if (IsPostBack)
                {
                    Response.Redirect("logon.aspx");
                }
            }

Share This

Leave a Reply

Your email address will not be published. Required fields are marked *


nine − = 3