我已將主題頁添加到我的腳本」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