Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Today, I was working on an application that required some Ajax magic. I had a very basic scenario set up in which I was simply inserting the database into the database using a simple form. I had some validation rules setup for my domain objects. If the validation rule was broken an exception was thrown and Global.asax Application_Error event caught the exception and uses Response.Write to write the exception on the screen. The problem occurred with the Response.Write method. Since, Response.Write simply writes the stream on the UI the UpdatePanel was getting messed up when rendering. If you want to try out this scenario take a look at the following code:

 <asp:UpdatePanel ID="up1" runat="server">
 <ContentTemplate>
 <asp:Button ID="Btn_Add" runat="server" Text="Throw Error" OnClick="Btn_AddClick" />
 <asp:Label ID="lblMessage" runat="server" />
 </ContentTemplate>
 </asp:UpdatePanel>

Nothing fancy! Just a simple Button control inside the UpdatePanel. Now take a look at the Button click event.

 protected void Btn_AddClick(object sender, EventArgs e)
        {
            Response.Write("Hello World");
        }

Just a simply Response.Write inside the Button click event. The Response.Write will cause a JavaScript exception to be thrown.

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near 'Hello World160|updat'.
 
So, how do you solve this problem. Well, you can simply remove Response.Write, Server.Transfer. But if you have your application setup long time ago and you don't want to go through all the code and remove those lines then you can suppress the JavaScript error. Check out the following code.

window.onerror = function(message,url,line)
{  
    document.getElementById("errorDiv").innerHTML = message;
   
    // logg the error
   
    return true;
}
 
The return true at the end will suppress the JavaScript error and will not block further processing.
 
The window.onerror will also catch any error you throw from the server side. This makes it easy to display the errors to the client. Maybe a client side error logger would be a good idea! What do you think?
 
 
 
 
 
 
Posted on Thursday, February 14, 2008 2:57 PM | Back to top


Comments on this post: Suppressing and Handling Errors from the Update Panel

# re: Suppressing and Handling Errors from the Update Panel
Requesting Gravatar...
Hei
i tried same coding but , i get same error "Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near 'Hello World160|updat'."
Left by shiyaz on Mar 21, 2008 11:55 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net