views:

34

answers:

1

What is the best way to ensure that events are rebound after an UpdatePanel callback?

Suggestions I've seen:

function pageLoad(sender, args){
    //bind events here.
}

or

use the .live(eventType, handler) method to initially bind the events

or

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(function() {
    //bind events here.
});
A: 

If the code is event driven, say click handlers (or any other bubbling event) for example, use .live().

If you need things that aren't event driven, e.g. plugins, widgets, etc. Then use add_endRequest(func) and run any plugins in there, so they run whenever an UpdatePanel finishes (gets new elements after a partial postback).

Nick Craver
If you needed to cover both scenarios, would you want to do both, or would it be acceptable to just go with `add_endRequest(func)`?
Homer
@Homer - I would use `.live()` whenever possible, only put the things that *need* to be run after a partial postback in `add_endRequest`, that's less to run on postback, and a much faster page for your user.
Nick Craver