I am updating my application from ATLAS to Ajax RC.
All my pages are inherited from my own WebPage subclass page that implements an OnError event and I have included the Script Manager with the OnAsyncPostBackError property in the master pages of the application.
When an exception is thrown within an Update Panel the Onerror event captures the exception and I haven't found a way to setup the AsyncPostBackErrorMessage. An alert message appears with the text:
Sys.WebForms.PageRequestManagerServerException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500
Any suggestions...?
Thanks...Antonio
?????ScriptManager.AsyncPostBackError += new EventHandler<AsyncPostBackErrorEventArgs>(ScriptManager_AsyncPostBackError);
Determine if it is asynchronous post back:
?????ScriptManager.IsInAsyncPostBack;
Get synchronous post back error message:
?????ScriptManager.AsyncPostBackErrorMessage;
Get asynchronous post back source:
?????ScriptManager.AsyncPostBackSourceElementID;
Wish the above can help you.
I am having the same issue... We have existing sites that uses a standard basepage the overrides the OnError to catch any page errors. It will log the error and then redirect to a nicely formatted error page to explain the error. This works fine, but when I am inside a AJAX call and I get an error, I also get the following error
Sys.WebForms.PageRequestManagerServerException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500
But if I remove my OnError Handler completely, then I will get the actual error in the javascript popup. How can I handle both the normal page errors and AJAX errors if just having the OnError seems to kill the AsyncPostBackError info?
Pat
changing UpdateMode from"Conditional" to "Always" solved my problem
I tried many ways to raise and catch the error with both OnError and OnAsyncPostBackError implemented. but no use. It always go to OnError handler.
Is someone suggests the complete code ... with master page, update panels on a page and onError and OnAsyncPostBackError events implemented?
Basically, I am looking to implement both handlers and distinguish to redirect Page request errors to OnError and Update panel requests errors to OnAsyncPostBackError;
Any help is appreciated in advance.
I found a way to do this ...
Put this snippet of code in your base Page codebehind ... this should capture every error that gets thrown.
1protected override void OnError(EventArgs e)2 {3 ScriptManager mgr = ScriptManager.GetCurrent(this);45 System.Exception ex =this.Server.GetLastError();67if (mgr.IsInAsyncPostBack)8 {9base.OnError(e);10 }11else12 {13this.Server.ClearError();1415base.OnError(e);1617 Response.Write(<html><head><title>Error</title></head><body>");18 Response.Write("<table class='Section'><tr class='SectionTitle'><td colspan='2'>Error Details</td></tr><tr><TD valign='top' class='FieldLabel' width='125px'>Error Message:</TD><td class='GenText'>");19 Response.Write(ex.Message);20 Response.Write("</TD></TR><TR><TD valign='top' class='FieldLabel'>Error Source:</TD><TD class='GenText'>");21 Response.Write(ex.Source);22 Response.Write("</TD></TR><TR><TD valign='top' class='FieldLabel'>Stack Trace:</TD><TD class='GenText'>");23 Response.Write(ex.StackTrace);24 Response.Write("</TD></TR><TR><TD colspan=\"2\"><a href=\"");25 Response.Write(Request.RawUrl);26 Response.Write("\">Return to previous page</a></td></tr></table></body></html>");27 Response.End();28 }29 }
Then in your master page (or whatever page) codebehind capture the AsyncPostBackError from the ScriptManager tag (making sure to hook the OnAsyncPostBackError in the aspx page to the ScriptManager1_AsyncPostBackError):
1protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)2 {3if (e.Exception.Data["ExtraInfo"] !=null)4 {5 ScriptManager1.AsyncPostBackErrorMessage =6 e.Exception.Message +7 e.Exception.Data["ExtraInfo"].ToString();8 }9else10 {11 ScriptManager1.AsyncPostBackErrorMessage ="The following error occurred: " + e.Exception.Message;13 }14 }
My updated code for handling OnError and OnAsyncPostBackError events ...
I took different approach like assigning event handlers early and then do accordingly. I have all my pages contain a Master Page and derived from Page Base ...MyAppPageBase
publicclassMyAppPageBase :Page{
protectedoverridevoid OnInit(EventArgs e)
{
bool AttachPageErrorEventHandler =true;
// Determine if it is a page request, then only attach page error event handler
try
{
if (this.Page !=null &&
ScriptManager.GetCurrent(this.Page) !=null &&
ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
{
// Note: We are not attaching any event handler for page error -->
this.Page.Error -=newEventHandler(MyAppPage_Error);
this.Page.Error +=newEventHandler(MyAppPage_UP_Error);
AttachPageErrorEventHandler =false;
}
}
catch { }
if (AttachPageErrorEventHandler)
{
this.Page.Error +=newEventHandler(MyAppPage_Error);
this.Page.Error -=newEventHandler(MyAppPage_UP_Error);
}
base.OnInit(e);
}
{
Exception exBase = Server.GetLastError().GetBaseException();
if (exBase.GetType().Name =="HttpRequestValidationException")
{
>>> Redirect to a page to show that you are using <script> tag as data; Malformed data
}
else// all other / generic exception
{
this.Context.AddError(exBase);
Server.ClearError();
Server.Transfer("~/Error.aspx");
Response.Flush();/*The line is added to cancel the event bubbleup */
Response.End();/*The line is added to cancel the event bubbleup */
}
}
protectedvoid TPPage_UP_Error(object sender,EventArgs e)
{
Exception exBase = Server.GetLastError().GetBaseException();
if (exBase.GetType().Name =="HttpRequestValidationException")
{
string str ="You have submitted the data with <script>tag or with malformed data";
Response.Write(string.Format("{0}|error|500|{1}|", str.Length, str ));
}
else// all other types of exception
{
Response.Write(string.Format("{0}|error|500|{1}|", exBase.Message.Length, exBase.Message));
}
}
}
so far it is working great!
No comments:
Post a Comment