Use jQuery and Ajax together with ASP.NET Master pages

I had previously posted about "jQuery does not work properly after ajax partial postback" and the best possible solution was to put all your jQuery code in pageLoad() function instead of document.ready(). This works great. But this will not work when you are using ASP.NET master page feature and you have jQuery code in master page as well as in content page. You can't use pageLoad function at both the places.

The reason is when you run the page, Master page and content page gets combined and we have single DOM. In single DOM, we can't have more than one pageLoad function.

The solution to this problem is to register another function in pageLoad() function of master page. And the registered function you can use in content page. See code.

<script type="text/javascript">
  function pageLoad() {
    // Master pageLoad() code.
 
    //Content Page Load function
    if(typeof contentPageLoad == 'function')
      contentPageLoad();
}

The contentPageLoad function will be executed on content page if you wish to include in your content page. If you don't include then it will not throw any error as we have checked in if condition.

Credit

Feel free to contact me for any help related to jQuery. I will gladly help you.



Responsive Menu
Add more content here...