我已将主题页添加到我的脚本”myscript.js“中。 然后,在内容页面中,我想在启动时加载myscript()(body onload)。ASP.NET内容页面:如何使用“body onload = myfunc()”在“body”上运行脚本“
我该怎么做?
我已将主题页添加到我的脚本”myscript.js“中。 然后,在内容页面中,我想在启动时加载myscript()(body onload)。ASP.NET内容页面:如何使用“body onload = myfunc()”在“body”上运行脚本“
我该怎么做?
要么使用
if (!IsStartupScriptRegistered (key))
{
String script = "<script type=\"text/javascript\">" +
"alert('Hello World');</" + "script>";
RegisterStartupScript(key, script);
}
或者你可以使用jQuery库和函数调用添加到的document.ready功能
$(document).ready(function(){
// Add your function call here
});
该窗体有onLoad事件。因此,使用标签在你的母版
我使用ContentPlaceHolder
在主页为此目的。这使得只有在需要时才可以在<head>
中包含特定的脚本。
这将onload事件客户端事件添加到主页的身体标记:
body id="PageBody" runat="server"
in the content page:
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("PageBody");
body.Attributes.Add("onload", "doSomething();");`
我不想使用它的母版,以避免运行脚本:
在母版页其他页面没有必要.. – stighy 2010-06-08 13:48:53
那么为什么不是Page.RegisterStartupScript或Page.RegisterClientScriptBlock? – Saurabh 2010-06-08 13:52:45
我没有把他们知道;) – stighy 2010-06-08 13:55:09