Just wanted to share an article which differentiates between document ready and pageLoad events and their usage to efficiently opt client end scripting and fix update panel issues, if applicable. Ignore, if you are already aware of.
To give a small background, I was working on few functionalities e.g. select all checkboxes, button click events from client side to avoid postbacks. As a jQuery fan, attached all these events using jQuery but wasn’t aware that the partial post backs (new HTMLs) won’t attach these events again and there should be something to force these events to get attached.
Went through few of articles and tried below solutions:
- Get instance of PageRequestManager and add your events/functions when the request ends. This would add events after ajaxpostbacks.
e.g:
- varprm = Sys.WebForms.PageRequestManager.getInstance();
- prm.add_endRequest(function(sender, e) {
Problem with this approach: This was working for all pagination, sorting functionalities but for async triggers
e.g: pagesize dropdown, the events weren’t getting attached.
- Use ScriptManager.RegisterStartupScript from server side and attach all JavaScript events.
- ScriptManager.RegisterStartupScript(this, this.GetType(), "Script", "AddEvents()”, true);
Problem with this approach: You need to copy-paste the same piece of server side code for all postback events.
- Per the article, you can use asp.net ajax’spageLoad() instead of $(document).ready() which would be called after every partial postback. Make sure to add only those events/functions that needs to be called after partial postback.
e.g: function pageLoad() { }
For those events, functions that needs to be called once per page load, the application init event or document ready can be used:
Sys.Application.add_init(function() { });
Hope, this would be helpful somewhere in future.