ラベル session の投稿を表示しています。 すべての投稿を表示
ラベル session の投稿を表示しています。 すべての投稿を表示

2012年3月28日水曜日

Response.Redirect and AsyncPostBackTrigger

I've got a "Previous" button on a webform that uses this code to take user to previous page:

Response.Redirect(Session.Item("PREVIOUSPAGE"))

I set the button as an AsyncPostBackTrigger. This error occurs when the button is clicked:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

If I change the button from AsyncPostBackTrigger to PostBackTrigger, it works fine.

Why won't this button work asynchronously? I've got 2 other buttons on the same page that are being used as AsyncPostBackTriggers with no issue.


I think it would not work because asynchronously - the Ajax enviroment is just expecting to work with the session variables of the current page, but you are trying to tell it to revert back to previous session ids with a redirect. A postback will work because it reloads or mimics a back button of the browser. Not saying it is not possible - I am wrong plenty of times - but it would make sense that that would be the behavior. You'll also get the same behavior if a session times out and the user tries to do a back button to a previous ajax enabled page.

What I would at least consider a better solution is leverage the ability with Ajax to load and unload controls dynamically. This way you can have 'paging' and still retain the benefits of the asyncrhonous enviroment.


There is an article on how to achieve that in the Scott Guthrie's blog:


http://weblogs.asp.net/scottgu/archive/2007/02/06/asp-net-ajax-goodies-documentation-download-back-button-support-new-animation-control.aspx

Juan

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...