1

IsClientScriptIncludeRegistered之前检查的好处有多大RegisterClientScriptInclude

文档中的示例

' Check to see if the include script is already registered.
If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then    
    cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
End If

其中指出:

请注意,如果删除了检查现有客户端脚本包含的逻辑,则在呈现的页面中仍然不会有重复的客户端脚本,因为 RegisterClientScriptInclude 方法会检查重复项。检查的好处是减少不必要的计算。

我想知道突出显示的句子。什么不必要的计算?

在我看来,情况正好相反。使用IsClientScriptIncludeRegistered之前RegisterClientScriptInclude应该/将检查注册的脚本两次(因此增加了不必要的计算而不是减少)。

我在这里想念什么?

4

1 回答 1

1

如果脚本全部准备好注册,则在字典中进行简单检查,这IsClientScriptIncludeRegistered是一种非常快速的检查,无需太多代码。

public bool IsClientScriptIncludeRegistered(Type type, string key)
{
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    return ((this._registeredClientScriptBlocks != null) && 
     this._registeredClientScriptBlocks.Contains(
        CreateScriptIncludeKey(type, key, false)));
}

RegisterClientScriptInclude另一方面,包含更多代码,直到达到现有的检查。

internal void RegisterClientScriptInclude(Control control, Type type, string key, string url)
{
    IScriptManager scriptManager = this._owner.ScriptManager;
    if ((scriptManager != null) && scriptManager.SupportsPartialRendering)
    {
        scriptManager.RegisterClientScriptInclude(control, type, key, url);
    }
    else
    {
        this.RegisterClientScriptInclude(type, key, url);
    }
}

internal void RegisterClientScriptInclude(Type type, string key, string url, bool isResource)
{
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    if (string.IsNullOrEmpty(url))
    {
        throw ExceptionUtil.ParameterNullOrEmpty("url");
    }
    string script = "\r\n<script src=\"" + HttpUtility.HtmlAttributeEncode(url) + "\" type=\"text/javascript\"></script>";
    this.RegisterScriptBlock(CreateScriptIncludeKey(type, key, isResource), script, ClientAPIRegisterType.ClientScriptBlocks);
}

internal void RegisterScriptBlock(ScriptKey key, string script, ClientAPIRegisterType type)
{
    switch (type)
    {
        case ClientAPIRegisterType.ClientScriptBlocks:
            this.RegisterScriptBlock(key, script, ref this._registeredClientScriptBlocks, ref this._clientScriptBlocks, false);
            break;

        case ClientAPIRegisterType.ClientScriptBlocksWithoutTags:
            this.RegisterScriptBlock(key, script, ref this._registeredClientScriptBlocks, ref this._clientScriptBlocks, true);
            break;

        case ClientAPIRegisterType.ClientStartupScripts:
            this.RegisterScriptBlock(key, script, ref this._registeredClientStartupScripts, ref this._clientStartupScripts, false);
            break;

        case ClientAPIRegisterType.ClientStartupScriptsWithoutTags:
            this.RegisterScriptBlock(key, script, ref this._registeredClientStartupScripts, ref this._clientStartupScripts, true);
            break;
    }
    if (this._owner.PartialCachingControlStack != null)
    {
        foreach (BasePartialCachingControl control in this._owner.PartialCachingControlStack)
        {
            control.RegisterScriptBlock(type, key, script);
        }
    }
}


private void RegisterScriptBlock(ScriptKey key, string script, ref ListDictionary scriptBlocks, ref ArrayList scriptList, bool needsScriptTags)
{
    if (scriptBlocks == null)
    {
        scriptBlocks = new ListDictionary();
        scriptList = new ArrayList();
    }
    if (!scriptBlocks.Contains(key))
    {
        Tuple<ScriptKey, string, bool> tuple = new Tuple<ScriptKey, string, bool>(key, script, needsScriptTags);
        scriptBlocks.Add(key, null);
        scriptList.Add(tuple);
    }
}
于 2013-03-03T03:05:38.927 回答