views:

15524

answers:

12

I have several input and option elements on my page, each (well almost) have an event attached to update some text on the page once they change. I use jQuery which is really really cool :)

I also use Microsofts Ajax framework, utilizing the UpdatePanel. The reason why I do that is that certain elements are created on the page based on some server-side logic. I don't really want to explain why I use the UpdatePanel - even if it could (it can with quite some effort) be rewritten to use only jQuery I still want that UpdatePanel.

You probably guessed it - once I have a postback on the UpdatePanel the jQuery events stops working. I actually was expecting this, since the "postback" is not really a new postback so my code in document.ready that binds the events won't be fired again. I also confirmed my suspicion by reading up on it in the jQuery help libraries.

Anyway I'm left with the problem of rebinding my controls after the UpdatePanel is done updating the DOM. I preferably need a solution that does not require adding more .js files (jQuery plug-ins) to the page but something as simple as being able to catch the UpdatePanel's 'afterupdating' where I can just call my method to rebind all the form elements.

+26  A: 

Since you're using ASP.NET AJAX, you'll have access to a pageLoad event handler, that gets called each time the page posts back, be it full or partial from an UpdatePanel. You just need to put the function in to your page, no hooking up is required.

function pageLoad(sender, args)
{
   if (args.get_isPartialLoad())
   {
       //Specific code for partial postbacks can go in here.
   }
}
Phil Jenkins
Right you are! Thanks :)
Per Hornshøj-Schierbeck
No problem - that's just one way of doing it btw. If you need more flexibility, check out the endRequest event on the PageRequestManager class.
Phil Jenkins
Lifesaver for me! Thanks
peiklk
Awesome! I replaced my jquery $(document).ready with your pageLoad (without the if block) and it solved my problem!
Chris
Caution: only one "pageLoad" function executes on page - the last one defined. So if actions that need to be executed reside on different controls - there are easier ways to achieve required behavior - like "Sys.Application.add_load".
Paulius
+3  A: 

You could use jQuery and event delegation. Basically hook events to containers rather than every element and query the event.target and run script based on that.

It has multiple benefits in that you reduce the code noise (no need to rebind). It is also easier on browser memory (less events bound in the DOM.)

Quick example here.

jQuery plugin for easy event delegation.

P.S I am 99% sure delegation will be in the jQuery core at the next release.

redsquare
+9  A: 
Sys.Application.add_load(initSomething);
function initSomething()
{
  // will execute on load plus on every UpdatePanel postback
}
Paulius
This one worked really good for me for making a jQuery ASP.Net control work inside of an update panel
Earlz
+1 this helped me as a work around for some other problem. Thank you very much.
Ismail
+13  A: 

Or you could check the latest jQuery's live functionality.

George
This was the best solution for me, as much of my jQuery markup is encapsulated inside several user controls on the page.
Kyle B.
+2  A: 

Use following code

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);

function pageLoaded(sender, args) {
    var updatedPanels = args.get_panelsUpdated();
    // check if Main Panel was updated 
    for (idx = 0; idx < updatedPanels.length; idx++) {
        if (updatedPanels[idx].id == "<%=upMain.ID %>") {
            rebindEventsForMainPanel();
            break;
        }
    }
}
shatl
+1  A: 

Are there any methods available to find the id of the object that triggered the event?

Patrick Dow
On the server or client end? If you're on the server side, you can see this answer here: http://stackoverflow.com/questions/1426088/making-a-difference-between-asyncpostbacks-in-nested-updatepanels-in-asp-net-ajax/1429215#1429215 on the client side you could hook into the beginRequest event, and inspect args.get_postBackElement() - see http://msdn.microsoft.com/en-us/library/bb397432.aspx
Zhaph - Ben Duguid
A: 

         <script type="text/javascript">
             function pageLoad() {

                 if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {


       }

            </script>

        </ContentTemplate>
    </asp:UpdatePanel>

into of the "if" you can put the code that you need execute every time that the updatepanel does AsyncPostBack.

CIM
A: 

Bind your events using jQuery's new 'live' method. It will bind events to all your present elements and all future ones too. Cha ching! :)

John Strickler
A: 

--->Awesome! I replaced my jquery $(document).ready with your pageLoad (without the if block) and it solved my problem!

I followed this and my datepicker started working after async-postback via update panel

This should have been a comment.
Ismail
A: 

Munchas gracias desde hace tiempo estaba buscando esto!

Jose Eyber Garcia
A: 

Great post! Thanks a lot for sharing

Niladri