动态向客户端注册脚本文件

//向客户端注册脚本文件

public static bool RegistScript(Page page, string path)
        {
            bool isRegisted = false;
            if (!string.IsNullOrEmpty(path))
            {
                path = page.ResolveUrl(path);
                if (!page.ClientScript.IsClientScriptIncludeRegistered(page.GetType(), path))//判断客户端是否注册了该脚本文件
                {
                    page.ClientScript.RegisterClientScriptInclude(page.GetType(), path, path);//向客户端注册指定的脚本文件
                  isRegisted = true;
                }
            }
            return isRegisted;
        }

//动态向客户端注册css文件,避免重复注册css文件导致IE重复注册页面出现css冲突

public static bool RegistCss(Page page, string pathCss)
{
    bool addCss = true;
    if (page.Header != null)
    {
        ControlCollection ccCollection = page.Header.Controls;
        if (ccCollection.Count > 0)
        {
            foreach (Control cItem in ccCollection)
            {
                if (cItem.GetType() == typeof(HtmlLink))
                {
                    HtmlLink hlLink = cItem as HtmlLink;
                    if (hlLink.Attributes["href"].ToString() == page.ResolveUrl(pathCss))
                    {
                        addCss = false;
                    }
                }
            }
        }
        if (addCss)
        {
            HtmlLink cssLink = new HtmlLink {
                Href = page.ResolveUrl(pathCss)
            };
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            page.Page.Header.Controls.Add(cssLink);
        }
    }
    return addCss;
}
原文地址:https://www.cnblogs.com/Minghao_HU/p/2767585.html