Click here to Skip to main content
15,885,655 members
Articles / UpdatePanel
Article

Using Sys.Application.add_load for rebinding events to Controls

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 24.1K   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --