2010-06-08 29 views

回答

3

要么使用

Page.RegisterStartupScript

if (!IsStartupScriptRegistered (key)) 
    { 
     String script = "<script type=\"text/javascript\">" + 
     "alert('Hello World');</" + "script>"; 
     RegisterStartupScript(key, script); 
    } 

或者你可以使用jQuery库和函数调用添加到的document.ready功能

http://jquery.com/

$(document).ready(function(){ 
    // Add your function call here 

}); 
0

该窗体有onLoad事件。因此,使用标签在你的母版

+0

我不想使用它的母版,以避免运行脚本:

在母版页

其他页面没有必要.. – stighy 2010-06-08 13:48:53

+0

那么为什么不是Page.RegisterStartupScript或Page.RegisterClientScriptBlock? – Saurabh 2010-06-08 13:52:45

+0

我没有把他们知道;) – stighy 2010-06-08 13:55:09

1

我使用ContentPlaceHolder在主页为此目的。这使得只有在需要时才可以在<head>中包含特定的脚本。

1

这将onload事件客户端事件添加到主页的身体标记:

body id="PageBody" runat="server" 

in the content page: 

HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("PageBody"); 

body.Attributes.Add("onload", "doSomething();");`