<?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>nPath Ltd. &#187; Ajax</title>
	<atom:link href="http://www.npathweb.com/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.npathweb.com</link>
	<description>Web Development Blog</description>
	<lastBuildDate>Mon, 22 Feb 2010 15:16:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ajax update panels and the errors that love them.</title>
		<link>http://www.npathweb.com/2009/02/05/ajax-update-panels-and-the-errors-that-love-them/</link>
		<comments>http://www.npathweb.com/2009/02/05/ajax-update-panels-and-the-errors-that-love-them/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 17:02:31 +0000</pubDate>
		<dc:creator>B.Harding</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://www.npathweb.com/blog/?p=20</guid>
		<description><![CDATA[I have been using ajax a lot lately in my current project. I have noticed an annoying quirk. When a runtime error happens the page appears to hang. temprarily dissabling the update panel quickly reveals that the page isn&#8217;t really stuck, but there is an error that ajax is ignoring.
I did some research and found [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using ajax a lot lately in my current project. I have noticed an annoying quirk. When a runtime error happens the page appears to hang. temprarily dissabling the update panel quickly reveals that the page isn&#8217;t really stuck, but there is an error that ajax is ignoring.</p>
<p>I did some research and found a few good examples of how to handle the errors but most were in C# and some didn&#8217;t work in Firefox. Imagine that!</p>
<p>Being a VB guy I decided to post a very stipped down sample of how it works.</p>
<p>The lowdown: The script manager is wired to an event handler that passes the exeption info to the client side java script.  Then we put some client side java script in to display the message.</p>
<pre><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">&lt;%@ Page Language=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"VB"</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">%</span>&gt;

&lt;!DOCTYPE html <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">PUBLIC</span> <span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span>&gt;

&lt;script runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"server"</span>&gt;

    <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Protected</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Sub</span> SuccessProcessClick_Handler(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ByVal</span> sender <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Object</span>, <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ByVal</span> e <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> System.EventArgs)
        <span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'a successful click</span>
        UpdatePanelMessage.Text <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> <span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"The asynchronous postback completed successfully."</span>

    <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">End</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Sub</span>

    <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Protected</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Sub</span> ScriptManager1_AsyncPostBackError(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ByVal</span> sender <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Object</span>, <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ByVal</span> e <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> System.Web.UI.AsyncPostBackErrorEventArgs)

        <span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'this creates an exception in the client side Javascript. the exeption will be handled on the client side.</span>
        ScriptManager1.AsyncPostBackErrorMessage <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> e.Exception.Message

    <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">End</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Sub</span>

    <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Protected</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Sub</span> ErrorProcessClick_Handler(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ByVal</span> sender <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Object</span>, <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ByVal</span> e <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> System.EventArgs)

        <span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'cause an error on this click</span>
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> x <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Integer</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> 0
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> y <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Integer</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> 10 <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span> x

    <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">End</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Sub</span>
&lt;/script&gt;

&lt;html xmlns=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"http://www.w3.org/1999/xhtml"</span>&gt;
&lt;head runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"server"</span>&gt;
    &lt;title&gt;Untitled Page&lt;/title&gt;
      &lt;style type=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"text/css"</span>&gt;
   #AlertDiv{
     position:relative;
     visibility: hidden;
     background-color:maroon;
     color:White;
     width:500px;

    }

    &lt;/style&gt;
&lt;/head&gt;
&lt;body &gt;
    &lt;form id=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"form1"</span> runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"server"</span>&gt;
        &lt;div&gt;
            &lt;asp:ScriptManager ID=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"ScriptManager1"</span> runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"server"</span>
                onasyncpostbackerror=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"ScriptManager1_AsyncPostBackError"</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span>&gt;

            &lt;script type=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"text/javascript"</span> language=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"javascript"</span>&gt;
              <span style="color: #008000;"> // this <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">is</span> the client side script that will handle the <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">error</span> passed from the server.</span>
                var divElem <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> <span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'AlertDiv';</span>
                var messageElem <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> <span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'AlertMessage';</span>
                var bodyTag <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> <span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'bodytag';</span>
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

               <span style="color: #008000;">//hide <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">or</span></span> show the <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">error</span></span> div</span>
                <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">function</span> ToggleAlertDiv(visString)
                {

                     var adiv <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> $<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">get</span>(divElem);
                     adiv.style.visibility <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> visString;

                }

                <span style="color: #008000;">//<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">reset</span></span> the <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">error</span></span> div</span>
                <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">function</span> ClearErrorState() {
                     $<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">get</span>(messageElem).innerHTML <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> <span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'';</span>
                     ToggleAlertDiv(<span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'hidden');                     </span>
                }
               <span style="color: #008000;"> //put the <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">error</span></span> text into the div</span>
                <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">function</span> EndRequestHandler(sender, args)
                {
                   <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">if</span> (args.get_error() !<span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> undefined)
                   {
                       var errorMessage <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> args.get_error().message;
                       args.set_errorHandled(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">true</span>);
                      ToggleAlertDiv(<span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'visible');</span>
                      <span style="color: #008000;">//errorMessage.<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">split</span></span><span style="color: #008000;">(<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;"><span style="color: #008000;">":"</span></span>,</span>2)[1] <span style="color: #008000;"><span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">=</span></span> </span><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">get</span></span> just the message <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">and</span></span> chop <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">off</span></span> the identifier before the :
                      //</span></span><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">The message iteself <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">is</span></span> the only usefull part <span style="color: #008000;"><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">to</span></span> </span>the <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #008000;">end</span></span> user.</span>
                      $<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">get</span>(messageElem).innerHTML <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span><span style="font-size: 11px; color: green; font-family: Courier New; background-color: transparent;">'Error:' + errorMessage.split(":",2)[1];</span>

                   }
                }
            &lt;/script&gt;
            &lt;asp:UpdatePanel runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"Server"</span> ID=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"UpdatePanel1"</span>&gt;
                &lt;ContentTemplate&gt;

                  &lt;div id=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"AlertDiv"</span>&gt;

                      &lt;div id=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"AlertMessage"</span>&gt;&lt;/div&gt;

                         &lt;<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">input</span> id=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"OKButton"</span> type=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"button"</span> value=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"OK"</span> runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"server"</span> onclick=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"ClearErrorState()"</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span>&gt;

                  &lt;/div&gt;

                        &lt;asp:Label ID=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"UpdatePanelMessage"</span> runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"server"</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span>&gt;&lt;br <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span>&gt;

                        &lt;asp:Button runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"server"</span> ID=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"Button1"</span> Text=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"Submit Successful Async Postback"</span>
                            OnClick=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"SuccessProcessClick_Handler"</span> OnClientClick=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"ClearErrorState()"</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span>&gt;
                        &lt;asp:Button runat=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"server"</span> ID=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"Button2"</span> Text=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"Submit Async Postback With Error"</span>
                            OnClick=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"ErrorProcessClick_Handler"</span> OnClientClick=<span style="font-size: 11px; color: #666666; font-family: Courier New; background-color: #e4e4e4;">"ClearErrorState()"</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span>&gt;

                &lt;/ContentTemplate&gt;
            &lt;/asp:UpdatePanel&gt;

     &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.npathweb.com/2009/02/05/ajax-update-panels-and-the-errors-that-love-them/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
