2011-04-25 183 views
2

我有一些數據庫的東西可以在我的Asp.net頁面的Page_Load中讀取。在Page_Load()中添加OnLoad()?

if (xxx == 1) 
    //Add OnLoad("clock()") 

如何做到這一點? 如何在頁面加載方法中爲JavaScript時鐘添加OnLoad-Method?

回答

4

月1日讓你的身體標籤servercontrol

<body runat="server" id="BodyTag"> 

第二參考它像

if (xxx == 1) 
    BodyTag.Attributes.Add("onload", "clock()"); 
2

使用RegisterClientScriptBlock方法的客戶端代碼添加到頁面:

Page.ClientScript.RegisterClientScriptBlock(
    this.GetType(), 
    "onload", 
    "window.onload = function(){ clock(); }", 
    true 
); 
1

註冊一個啓動腳本在Page_Load。 ClientScriptManager(http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx)或ScriptManager(h ttp://msdn.microsoft.com/en-us/library/bb310408.aspx)...以適用者爲準。

string clockStuff = 
    @"function callClockFunction(){ 
    Sys.Application.remove_load(callClockFunction); 
    clock(); } 
    Sys.Application.add_load(callClockFunction);"; 
ScriptManager.RegisterStartupScript(this, this.GetType(), "callClockFunction", clockStuff, true);