<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wagner Danda Weblog &#187; exception handling</title>
	<atom:link href="http://www.wagnerdanda.me/tag/exception-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wagnerdanda.me</link>
	<description>.NET, OpenSource, Web 2.0 and other projects</description>
	<lastBuildDate>Fri, 06 Aug 2010 04:28:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET Ajax Error/Exception Handling &#8211; the simple way</title>
		<link>http://www.wagnerdanda.me/2010/01/asp-net-ajax-updatepanel-error-exception-handling-the-simple-way/</link>
		<comments>http://www.wagnerdanda.me/2010/01/asp-net-ajax-updatepanel-error-exception-handling-the-simple-way/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 01:36:56 +0000</pubDate>
		<dc:creator>Wagner Danda</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exception handling]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[UpdatePanel Error Handling]]></category>

		<guid isPermaLink="false">http://www.wagnerdanda.me/?p=134</guid>
		<description><![CDATA[A lot has been said about error/exception handling with ASP.NET Ajax pages, more specifically, how to properly handle and display error messages inside an UpdatePanel. For instance, Scott Gu wrote this sometime ago:
You can now optionally [...] intercept any error message sent back from the server, and perform custom client-side actions as a result [...]. [...]]]></description>
			<content:encoded><![CDATA[<p>A lot has been said about error/exception handling with ASP.NET Ajax pages, more specifically, how to properly handle and display error messages inside an UpdatePanel. For instance, <a href="http://weblogs.asp.net/scottgu/">Scott Gu</a> wrote this sometime ago:</p>
<blockquote><p><em>You can now </em><strong><em>optionally </em></strong><em>[...] intercept any error message sent back from the server, and perform custom client-side actions as a result [...]. (<a href=" http://weblogs.asp.net/scottgu/archive/2006/10/29/tip_2f00_trick_3a00_-handling-errors-with-the-updatepanel-control-using-asp.net-ajax.aspx">source</a></em><em>)</em></p></blockquote>
<p><strong>Well, I disagree with &#8220;optionally&#8221;.</strong> As far as I understand, <strong>if you don&#8217;t explicitly intercept</strong> the error/exception sent back from the server from within an UpdatePanel, your user will get an unfriendly javascript error like this:</p>
<p><a href="http://www.wagnerdanda.me/wp-content/uploads/2010/01/error_on_page1.jpg"><img class="aligncenter size-full wp-image-143" title="asp.net ajax updatepanel error on page message" src="http://www.wagnerdanda.me/wp-content/uploads/2010/01/error_on_page1.jpg" alt="" width="593" height="67" /></a></p>
<p><strong>So what can you do to fix this? </strong>Easy, just add this piece of Javascript code to your Masterpage (or to any page if you don&#8217;t have/use a Masterpage):</p>
<pre class="brush: jscript; highlight: [1,13,19];">
&lt;%-- This script must be placed after the form declaration (i.e. &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;) --%&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    Sys.Application.add_load(AppLoad);

    function AppLoad() {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
    }

    function EndRequest(sender, args) {
        // Check to see if there's an error on this request.
        if (args.get_error() != undefined) {

            var msg = args.get_error().message.replace(&quot;Sys.WebForms.PageRequestManagerServerErrorException: &quot;, &quot;&quot;);

            // Show the custom error.
            // Here you can be creative and do whatever you want
            // with the exception (i.e. call a modalpopup and show
            // a nicer error window). I will simply use 'alert'
            alert(msg);

            // Let the framework know that the error is handled,
            //  so it doesn't throw the JavaScript alert.
            args.set_errorHandled(true);
        }
    }
&lt;/script&gt;
</pre>
<p><strong>Now you should get error messages like this (javascript alert): </strong><a href="http://www.wagnerdanda.me/wp-content/uploads/2010/01/ooops_error.jpg"><img class="aligncenter size-full wp-image-162" title="ooops_error" src="http://www.wagnerdanda.me/wp-content/uploads/2010/01/ooops_error.jpg" alt="" width="306" height="193" /></a></p>
<p>This is exactly how exceptions used to be displayed when thrown inside an UpdatePanel before .NET 3.5.</p>
<p><strong>Note: I do understand the benefits of a the new approach</strong>. It gives you flexibility to present the error message any way you want, before your hands where quite tied to this simple Javascript alert window.</p>
<p>As a matter of fact, I&#8217;ve implemented a nice <a href="http://www.asp.net/ajax/ajaxcontroltoolkit/samples/modalpopup/modalpopup.aspx">ModalPopup</a> and have ensure that only user-friendly messages are actually shown: only messages of a certain type (i.e. UIMessageException) are forwarded to the user without any cleanup/transformation.</p>
<p>References:</p>
<ul>
<li><a href="http://www.jankoatwarpspeed.com/post/2008/06/02/Exception-handling-best-practices-in-ASPNET-web-applications.aspx ">Exception handling best practices in ASP.NET web applications</a></li>
<li><a href="http://www.asp.net/(S(ywiyuluxr3qb2dfva1z5lgeg))/learn/ajax-videos/video-9184.aspx">How Do I: Customize Error Handling for the ASP.NET AJAX UpdatePanel</a></li>
<li>MVP blog: <a href="http://msmvps.com/blogs/luisabreu/archive/2006/10/29/UpdatePanel_3A00_-having-fun-with-errors.aspx">UpdatePanel: having fun with errors</a></li>
<li>MSDN post (be careful, not 100% correct): <a href="http://msdn.microsoft.com/en-us/library/bb398934.aspx">Customizing Error Handling for ASP.NET UpdatePanel Controls</a></li>
</ul>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wagnerdanda.me/2010/01/asp-net-ajax-updatepanel-error-exception-handling-the-simple-way/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
