/// <summary> /// 将 Code 属性中的脚本作为一个代码块写入指定的目标中, 如果指定关键字的代码块已经在目标中存在, 则不写入. /// </summary> /// <param name="holder">表示脚本写入目标的对象, 可以是 ASPXScriptHolder 或者 RazorScriptHolder, 分别表示 aspx 和 cshtml 页面.</param> /// <param name="key">用于在生成过程中区分代码块的关键字.</param> /// <param name="option">生成选项, 目前 EndResponse 无效, Startup 表示在页面尾部写入代码块, 而 None 表示在页面头部写入. (此选项对 Razor 无效)</param> public void Build ( ScriptHolder holder, string key, ScriptBuildOption option ) #endif { if ( null == holder || this.code == string.Empty || IsBuilt ( holder, key, option ) ) return; string type; switch ( this.scriptType ) { case ScriptType.JavaScript: default: type = "text/javascript"; break; } string script; // HACK: 可能需要添加 V5 #if V4 if ( option.HasFlag ( ScriptBuildOption.OnlyCode ) ) #else if ( ( option & ScriptBuildOption.OnlyCode ) == ScriptBuildOption.OnlyCode ) #endif script = this.code; else script = string.Format ( "<script language='{0}' type='{1}'>\n{2}\n</script>", this.scriptType.ToString ( ).ToLower ( ), type, this.code ); key = MakeKey ( key ); // HACK: 可能需要添加 V5 #if V4 if ( option.HasFlag ( ScriptBuildOption.Startup ) ) #else if ( ( option & ScriptBuildOption.Startup ) == ScriptBuildOption.Startup ) #endif holder.RegisterStartupScript ( key, script ); else holder.RegisterClientScriptBlock ( key, script ); // if ( option.HasFlag ( ScriptBuildOption.EndResponse ) && null != page.Response ) // page.Response.End (); }
/// <summary> /// Determine whether the code block with the specified name already exists? /// </summary> /// <param name="holder">Container page of the code block, which can be ASPXScriptHolder or RazorScriptHolder.</param> /// <param name="key">Keyword used to differentiate between code blocks in the build process.</param> /// <param name="option">Script generation options, which can be None, Startup. (Not valid for Razor)</param> /// <returns>The code block exist?</returns> #elif HANS /// <summary> /// 判断指定名称的代码块是否已经存在? /// </summary> /// <param name="holder">代码块所在的容器页面, 可以是 ASPXScriptHolder 或者 RazorScriptHolder.</param> /// <param name="key">用于在生成过程中区分代码块的关键字.</param> /// <param name="option">脚本的生成选项, 可以是 None, Startup. (对 Razor 无效)</param> /// <returns>是否存在代码块?</returns> #endif #endregion public static bool IsBuilt ( ScriptHolder holder, string key, ScriptBuildOption option ) #endif { if ( null == holder ) return false; // HACK: You may need to add the V5 #if V4 if ( option.HasFlag ( ScriptBuildOption.Startup ) ) #else if ( ( option & ScriptBuildOption.Startup ) == ScriptBuildOption.Startup ) #endif return holder.IsStartupScriptRegistered ( MakeKey ( key ) ); else return holder.IsClientScriptBlockRegistered ( MakeKey ( key ) ); }