Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using Sys.Application.add_load for rebinding events to Controls

0.00/5 (No votes)
6 May 2011 1  
jQuery helps a lot in order to bind events to the control across the browser. But when using the Asp.Net Ajax UpdatePanel, there are some issues

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

jQuery helps a lot in order to bind events to the control across the browser. But when using the Asp.Net Ajax UpdatePanel, there are some issues doing that.

Here is a sample code of document.ready function in jQuery which helps to bind he events to the controls

 $(document).ready(function() {
   // put all your jQuery goodness in here.
 });

 Consider using a updatepanel in your webform, and you have a button in that.
Suppose you have a div element in your webform, and you want to keep it hiding until is asked to show.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
              <div id='rightSide'>Some content.......</div>
              <asp:Button ID="Button1" runat="server" Text="Button" />
        </ContentTemplate>

 </asp:UpdatePanel>

You can use the following code for above use:  $(document).ready(function() {

   $('div#rightSide').hide();

 });

This code will hide the div until a partial postback is happened from the updatepanel. This is because, the content inside the update panel will get refreshed in each partial rendering. And at that time the document.ready function will not be executed.

So how we maintain the context...

You can add the following code within the updatepanel
Sys.Application.add_load(<function>);

That is :
redesign your document.ready function to the following one:
 function BindEvents(){
   $(document).ready(function() {
      $('div#rightSide').hide();\
    });

}

and then add this function in the load of the application, that is: <asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>
        <script>Sys.Application.add_load(BindEvents);</script>
        <div id='rightSide'>Some content.......</div>
            <asp:Button ID="Button1" runat="server" Text="Button" />
     </ContentTemplate>
</asp:UpdatePanel>


This is make the document.ready fire each time when the partial rendering within the updatepanel happens. And thus solves the problem.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here