2012年3月26日月曜日

response.redirect cannot be called in a callback call

And I really need to.. anyone can tell me the reason why? The problem is that in the callback I call some session variables and if the session has expired, instead of throwing an exception, I want to redirect the user to the login page.

Any idea will be awesome!!

Thanks

check for session before use it

Session["Value"] ! = null

Also you can redirect the user to the login page for example if the session has been expired by handling Session_End method in the Global.asax


Can you post the code of your callback function?


Well... maybe I need to explain myself better. I have a base class from which all my ASPX file inherit; in this class I have this code:

1 protected override void OnPreInit(EventArgs e)

2 {
3 if (Context.Session !=null)
4 {
5 if (Session.IsNewSession)
6 {
7 string cookieHeader = Request.Headers["Cookie"];
8 if (!String.IsNullOrEmpty(cookieHeader) && cookieHeader.IndexOf("ASP.NET_SessionId") >= 0)
9 {
10 if (Page.IsCallback)
11 {
12 //This is where I need to redirect the user in the callback
13 }
14 else
15 {
16 Response.Redirect("~/SessionExpiredPage.aspx",true);
17 }
18 }
19 }
20 }
21
22 base.OnPreInit(e);
23 }

So I basically check if the session has expired and if so, I redirect the user to a session expired page. I want to do it in the same class, but I have not found a way to stop the callback, redirect or anything... any advice is welcome.

Thanks in advance


What error are you getting?

hi, i'm assuming your talking about a page method callback? if so I don't think there is anyway to re-direct the whole page in the way you describe. You would just end up re-directing the callback call if you get what I mean. Your gonna have to handle this on the client-side

Couple of ways you could acheive what your trying to do.(these are untested theories)

if you using an updatepanel, if the session has timed out, add this to the response

string script = string.Format("document.location.href = '{0}');","loginPage.aspx");

scriptManager.RegisterClientScriptBlock(Page,typeof(Page),"redirect", script,true);

if your using a page method callback it's a little more difficult

if the session has expired you could use a cookie

HttpCookie objCookie = new HttpCookie("MyCookie");
Response.Cookies.Clear();
Response.Cookies.Add(objCookie);
objCookie.Values.Add("sessionExpired","true");
DateTime dtExpiry = DateTime.Now.AddDays(1);
Response.Cookies["MyCookie"].Expires = dtExpiry;

then in your client-side callback function, check for the existance of this cookie and re-direct using document.location.href (but be sure to clear the timeout cookie)

Cheers Si


thanks for the answer... and you're right.. there is no way to make that callback redirect...

0 件のコメント:

コメントを投稿