<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="http://involvenevolve.com/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Sudarshan's Blog</title>
    <link>http://involvenevolve.com/</link>
    <description>My Thoughts, Findings &amp; Experiences</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <item>
      <guid isPermaLink="false">1102</guid>
      <link>http://involvenevolve.com/archive/aspnet-ajax-async-call-doesnt-fire-jquerys-documentready-event-solution/</link>
      <category>Asp.Net</category>
      <category>AJAX</category>
      <title>ASP.NET AJAX async call doesn't fire jQuery's 'document.ready()' event: Solution</title>
      <description>&lt;p&gt;On our current project we were using following technologies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;ASP.NET&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;jQuery&lt;/strong&gt;: for DOM manipulation at client side&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;jQuery plugins&lt;/strong&gt; like validator plugin, cleditor plugin, msked input plugin etc.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ASP.NET AJAX&lt;/strong&gt;: For doing asynchronous calls&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Before introducing AJAX functionality on the page, all the jQuery plugins were working fine.But, as we implement AJAX functionality on the page, all the plugins stopped working.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BIG QUESTION&lt;/strong&gt;, why?&lt;/p&gt;
&lt;p&gt;Lets examine working on any jQuery plugin. We need to perform some operations inside the jQuery's 'document.ready()' event.&lt;/p&gt;
&lt;p&gt;Here is the example for masked input plugin:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"&gt;$(document).ready(&lt;span style="color: #0000ff;"&gt;function&lt;/span&gt;() {&lt;br /&gt;    &lt;span style="color: #008000;"&gt;// Attaches masks to page controls&lt;/span&gt;&lt;br /&gt;    $(&lt;span style="color: #006080;"&gt;'#&amp;lt;%= txtSsn.ClientID %&amp;gt;'&lt;/span&gt;).mask(&lt;span style="color: #006080;"&gt;"999-99-9999"&lt;/span&gt;);&lt;br /&gt;    $(&lt;span style="color: #006080;"&gt;'#&amp;lt;%= txtBirthDate.ClientID %&amp;gt;'&lt;/span&gt;).mask(&lt;span style="color: #006080;"&gt;"99-99-9999"&lt;/span&gt;);&lt;br /&gt;});&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This code get executed every time page is loaded.&lt;/p&gt;
&lt;p&gt;Now, when we do asynchronous call using ASP.NET AJAX, only update panel's content get refreshed and not the whole page. This is why jQuery's '&lt;strong&gt;document.ready()&lt;/strong&gt;' event doesn't get fired. Because of this the masking associated with page controls gets removed.&lt;/p&gt;
&lt;p&gt;But, this is so common situation and what to do?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;There are 2 different ways that I know:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Make use of async call end event which is provided by ASP.NET AJAX. I don't prefer this way because we need to explicitly register and handle async event.&lt;/li&gt;
&lt;li&gt;Implement jQuery '&lt;strong&gt;AjaxReady&lt;/strong&gt;' event . (&lt;em&gt;Thanks to unknown person who posted this solution on some forum, but he missed out to mention how exactly we should use it&lt;/em&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So, we will prefer 2nd way.&lt;/p&gt;
&lt;p&gt;This solutions involves following steps:&lt;/p&gt;
&lt;h4&gt;Register event into jQuery like this&lt;/h4&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"&gt;jQuery.fn.extend({&lt;br /&gt;    AjaxReady: &lt;span style="color: #0000ff;"&gt;function&lt;/span&gt; (fn) {&lt;br /&gt;        &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (fn) {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; jQuery.&lt;span style="color: #0000ff;"&gt;event&lt;/span&gt;.add(&lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;[0], &lt;span style="color: #006080;"&gt;"AjaxReady"&lt;/span&gt;, fn, &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;);&lt;br /&gt;        } &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; {&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;var&lt;/span&gt; ret = jQuery.&lt;span style="color: #0000ff;"&gt;event&lt;/span&gt;.trigger(&lt;span style="color: #006080;"&gt;"AjaxReady"&lt;/span&gt;, &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;, &lt;span style="color: #0000ff;"&gt;this&lt;/span&gt;[0], &lt;span style="color: #0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;);&lt;br /&gt;            &lt;span style="color: #008000;"&gt;// if there was no return value then the even validated correctly  &lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (ret === undefined)&lt;br /&gt;                ret = &lt;span style="color: #0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; ret;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;});&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This code should present into your &lt;strong&gt;*.js&lt;/strong&gt; file or inside '&lt;strong&gt;&amp;lt;script language="javascript"&amp;gt;&lt;/strong&gt;' tag&lt;/p&gt;
&lt;h4&gt;Add following script within update panel's 'ContentTemplate'&lt;/h4&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"&gt;&amp;lt;asp:UpdatePanel ID=&lt;span style="color: #006080;"&gt;"upnlInsuranceCarrier"&lt;/span&gt; runat=&lt;span style="color: #006080;"&gt;"server"&lt;/span&gt;&amp;gt;&lt;br /&gt;    &amp;lt;ContentTemplate&amp;gt;&lt;br /&gt;        &lt;span style="color: #008000;"&gt;// Below script is very important&lt;/span&gt;&lt;br /&gt;        &amp;lt;script type=&lt;span style="color: #006080;"&gt;"text/javascript"&lt;/span&gt;&amp;gt;&lt;br /&gt;            prm = Sys.WebForms.PageRequestManager.getInstance();&lt;br /&gt;            prm.add_endRequest(EndRequest);&lt;br /&gt;            &lt;span style="color: #0000ff;"&gt;function&lt;/span&gt; EndRequest(sender, args) {&lt;br /&gt;                $(document).AjaxReady();&lt;br /&gt;            }&lt;br /&gt;        &amp;lt;/script&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;Define your $(document).AjaxReady() event like this&lt;/h4&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"&gt;$(document).AjaxReady(&lt;span style="color: #0000ff;"&gt;function&lt;/span&gt; () {&lt;br /&gt;    &lt;span style="color: #008000;"&gt;// Add code which needs to be executed after async call&lt;/span&gt;&lt;br /&gt;});&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;Finally your code will look like&lt;/h4&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre style="font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"&gt;$(document).ready(&lt;span style="color: #0000ff;"&gt;function&lt;/span&gt; () {&lt;br /&gt;    setup();&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;$(document).AjaxReady(&lt;span style="color: #0000ff;"&gt;function&lt;/span&gt; () {&lt;br /&gt;    setup();&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;function&lt;/span&gt; setup() {&lt;br /&gt;    $(&lt;span style="color: #006080;"&gt;'#&amp;lt;%= txtSsn.ClientID %&amp;gt;'&lt;/span&gt;).mask(&lt;span style="color: #006080;"&gt;"999-99-9999"&lt;/span&gt;);&lt;br /&gt;    $(&lt;span style="color: #006080;"&gt;'#&amp;lt;%= txtBirthDate.ClientID %&amp;gt;'&lt;/span&gt;).mask(&lt;span style="color: #006080;"&gt;"99-99-9999"&lt;/span&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Now, masked input plugin will work fine &lt;span style="color: #0000ff;"&gt;in&lt;/span&gt; async call &lt;span style="color: #0000ff;"&gt;as&lt;/span&gt; well&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Happy Programming!&lt;/p&gt;</description>
      <pubDate>Wed, 06 Oct 2010 00:56:13 Z</pubDate>
      <a10:updated>2010-10-06T00:56:13Z</a10:updated>
    </item>
  </channel>
</rss>