i have 3 usercontrols who in each i used pageLoad function in javascript and those usercontrols are in a page and page has himself pageLoad function. but only pageLoad function of last usercontrol who i registered in page is fired and others not fired
开发者_如何学JAVA<script type="text/javascript">
...
function pageLoad() {
alert('page load - uc1');
.....
}
...
please help me how to fire all functions
thanks
You may define javascript function for each control and execute it on Application.Init and PageRequestManager.EndRequest events:
Client side:
function controlSpecificPageLoadFunction(){ alert("Hello, pageLoad!"); }
Code behind:
if(!IsPostBack)
{
var script = "Sys.Application.add_init(controlSpecificPageLoadFunction); Sys.WebForms.PageRequestManager.getInstance().add_endRequest(controlSpecificPageLoadFunction)";
ScriptManager.RegisterStartupScript(this, this.GetType(), "thisControlPageLoad", script, true);
}
There is a paradigm called unobtrusive JavaScript that has been getting more and more popular. You should read up on it.
http://labs.adobe.com/technologies/spry/articles/best_practices/separating_behavior.html
http://en.wikipedia.org/wiki/Unobtrusive_JavaScript
Basically instead of saying onload="myfunction()"
, you wire up the handlers in the JS itself...
document.getElementById('myElement').addEventListener('click', myfunction)
function myfunction() { ... }
In your case, it would be something like
document.body.addEventListener('load', myfunction);
This has its own pitfalls however. I would strongly recommend using a library like jQuery, MooTools, Dojo or EXT instead of writing JavaScript from scratch.
精彩评论