The Problem:
Those looking to take advantage of .NET’s Panel control, untilizing a Scriptmanager to create asyncronious post backs, while at the same time using jQuery’s $(document).ready() function will encounter an issue.
During the initial page load any functionality that takes place inside the $(document).ready() completes successfully. However, because $(document).ready() doesn’t get called during a partial-page post back, the same events won’t take place during a partial page load. Essentially, this means that any elements with bound handlers inside a Update Panel will lose that functionality, and any functions that are called once the DOM is ready won’t be called once the Script Manager has it’s way with the post back event.
For binding elements, this isn’t difficult to account for. jQuery’s .live() method allows the intelligent ‘re-binding’ of elements with ease:
// Use
$(document).ready(function(){
$("a#my-element").live('click', function(){
alert("Sexy Beast Baby!"); // Will bind on initial page load and each subsequent DOM occurence
});
});
// As Oppossed To
$(document).ready(function(){
$(a#my-element).click(function(){
alert("Sexy Beast Baby!"); // Will only bind on initial page load.
});
});
This simple change will account for any element binding that needs to take place, but doesn’t provide a method for isolated javascript funtions to be called inside of $(document).ready(). As of the time of posting, there is no jQuery method that accounts for this.
With jQuery powerless to handle the situation, I looked to the .NET framework for a solution. Luckily there’s a pretty slick resolution to this issue.
The Fix:
When partial rendering is enabled on the scriptmanager, it makes the instance of the Page Request available. Utilizing it’s .add_EndRequest method will solve our issue. The .add_EndRequest method does exactly what it sounds like it would do; whatever takes place inside this method occurs after the partial page post back takes place providing a perfect place to call any additional functionality needed at page load. For Example:
In the mark up:
<!--- The ScriptManager should enable Partial Rendering. It's False by default: ---> <asp:ScriptManager ID="MyScriptManager" runat="server" EnablePartialRendering="True"></asp:ScriptManager>
In the javascript:
// Gets the Partial Page Load Instance and executes my "load_async_doc_ready" when it's complete
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(load_asyc_doc_ready);
// This function gets called once the partial page is done rendering
function load_async_doc_ready(sender, args) {
// If this fails, everything stops. Should only fail in the event of a serious error.
if (args.get_error() == undefined) {
// A 'second' instance of jQuery's document ready
$(document).ready(function() {
// Do what ever you need to here.
set_init_values();
// etc...
});
}
}
And to think… I initially thought .NET and jQuery wouldn’t get along with each other.










I work on a project that has lots of partial views that are loaded when needed. Just about each view has their own set of JavaScript code, one or two with its own .live() attachment for additional partial views. The problem I hit was that when the partial view with .live() was loaded additional times, the events doubled, tripled, etc. To prevent this from continuing, .die() is needed to kill the .live() event handler. Those jQuery guys, what don’t they think of!?
That is interesting about the need for .die(). I’m going to have to recheck and make sure .live() isn’t firing off when it shouldn’t be.
I’m curious, how do you handle the js load with each section’s partial load?
We use .load() to update the element in the selector. It’s designed poorly at the time because we could avoid any redundant .live() calls by moving the JS code into the partial view that really needs the event handlers, but that’s in theory. Since the site has no page loads (one like gmail), I’m not entirely sure how jQuery would handle the .live() call if it’s moved to the other partial view. It could retain the handler and I have to .die() anyway.
I have an issue with jquery live() and die(). I have an update panel and 2 user controls inside. i am toggling visibility on them. Each user control has its own javascript file which is included in code behind Page_load of each. in the JS with jquery has #(“a”).live(“click”, function(e){});. As mentioned here once the first user control loads it works fine, but now switching to the other user control loads the other JS. My issue is that when i click on “a” on second user control both jquery $(“a”).live() on each user control is firing..!!…What a mess…..I tried calling #(“a”).die(“click”); on Load but it kills everything from that point on….IS there a smart way of calling die and removing it so that new JS won’t get affected…? I thank you for your reply…. note: I can not change the selector $(“a”) as the data HTML is coming from a Database.
Hey Manuel,
Sorry It’s taken me a bit to respond, if you’re still having this issue, I can take a look at it for you.
That does sound like a frustrating issue. I can venture a guess as to what’s going on, but it might be better if I could see a cold sample?