I have pages that contain user controls that also contain other user controls.
I had the same issue with events and the page rendering stack with this structure. To resolve this, I placed user control specific event management within a separate .js file such as mycontrol.js and referenced that .js within the .ascx file (put the link to it there).
I also had a situation where I did not have the user control rendered until placed within the markup (a sub-control) and thus the events could not be placed at that point. I handled this, by loading the user controls javascript within my javascript thus:
/* function to allow inclusion of other files
Included this way to allow addition AFTER the page renders the control - that is, fake inclusion in this file
*/
function includeJS(jsPath)
{
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", jsPath);
document.getElementsByTagName("head")[0].appendChild(script);
};
includeJS('Js/secondusercontrol.js');
- Separate script code for user controls and link within user controls
- Include .js files using script if latency is an issue
- put all "base" script and global objects (if needed) within the page .js file
- use .live() and .delegate()
EDIT: one other option which may work for you I forgot initially is to use the .live()
and/or the .delegate()
functionality.