2010-07-21 52 views
1

一個小問題,我希望承認一個簡單的答案,我會踢自己不注意。當調用ScriptManager.RegisterStartupScript時,'Type'參數的意義是什麼?

所以,當我們的RegisterStartupScript

public static void RegisterStartupScript(
    Control control, 
    Type type, 
    string key, 
    string script, 
    bool addScriptTags 
) 

以下過載,我們必須提供一個類型以及一個控制。現在,我可以看到指定控件的意義 - 只要控件是部分頁面呈現的一部分,腳本就會被推出。但是Type參數的意義是什麼?通常只是將其設置爲控件的類型。這實際上是MSDN提出的建議:

control 
    Type: System.Web.UI..::.Control 
    The control that is registering the client script block. 

type 
    Type: System..::.Type 
    The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script. 

那麼爲什麼我們必須指定它呢?大概不僅僅是爲了讓.Net框架免費檢索運行時類型本身。

回答

2

通常類型是註冊腳本的頁面或控件的類型。這只是防止兩個不同控件使用相同密鑰註冊不同腳本的一種方法。從MSDN

摘錄:

客戶端腳本唯一標識,通過它的鍵和類型 。與 相同的密鑰和類型的腳本被認爲是 重複。 給定類型和密鑰對的 只有一個腳本可以是 註冊的頁面。嘗試 註冊腳本已經是 註冊不會創建腳本的重複 。

+0

哦,所以控件本身不是唯一標識腳本的部分內容?因爲您可能有一個與特定類型的控件相關的類似腳本。好的我知道了。很酷,謝謝。 – Yellowfog 2010-07-21 15:53:32

相關問題