2010-06-08 72 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();");`