是否有注册的&lt内部脚本System.Web.UI.ClientScriptManager方法; HEAD>标签? [英] Is there a System.Web.UI.ClientScriptManager method that registers scripts inside the <head> tag?

查看:236
本文介绍了是否有注册的&lt内部脚本System.Web.UI.ClientScriptManager方法; HEAD>标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是严格确定把<脚本>在体内标签,但在整洁的利益,我想使用 System.Web.UI.ClientScriptManager 注册在℃的脚本; HEAD> 我的网页。有没有做到这一点的方法?

I know it's strictly OK to put <script> tags in the body, but in the interest of neatness I'd like to use System.Web.UI.ClientScriptManager to register a script in the <head> of my page. Is there a method to accomplish this?

先谢谢了。

推荐答案

在这种情况下,我通常是在我的母版页的标签添加的ContentPlaceHolder。

In these cases I usually add a ContentPlaceHolder in the tag of my Master Page.

另外,我使用的方法(通常在实用类或PageBase类)把剧本字符串在ASP.Net背景下,像这样一个列表,并将其存储:

Alternately I've used a method (usually in a utility class or PageBase class) that puts the script string in a List and stores it in the ASP.Net Context like so:

            List<string> javaScriptUrls = new List<string>();

            url = url.ToLower();

            javaScriptUrls = Context.Items[JS_KEY] as List<string>;

            if (javaScriptUrls == null)
            {
                javaScriptUrls = new List<string>();

                javaScriptUrls.Add(url);
            }
            else
            {
                if (!javaScriptUrls.Contains(url))
                    javaScriptUrls.Add(url);
            }

            Context.Items[JS_KEY] = javaScriptUrls;

然后在母版的preRender,它从上下文中读取名单,并在标题中建立的标签。

Then OnPreRender of the MasterPage, it reads this List from the Context and builds tags in the header.

            List<string> _javaScript = Context.Items[JS_KEY] as List<string>;

            foreach (string js in _javaScript)
            {
                HtmlGenericControl script = new HtmlGenericControl();
                script.TagName = "script";
                script.Attributes.Add("type", "text/javascript");
                if (js.StartsWith("~/"))
                    script.Attributes.Add("src", head.ResolveUrl(js));
                else
                    script.Attributes.Add("src", js);

                head.Controls.Add(script);

                AddHeaderLineBreak(head);
            }

这篇关于是否有注册的&lt内部脚本System.Web.UI.ClientScriptManager方法; HEAD&GT;标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆