我如何判断

就像标题所说,我试图找出是否需要包含我的 ASP.net UserControl 需要工作的脚本库。我不想在每个页面多次包含它,但我希望我的控件能够在同一页面上多次使用。

如何在控件的代码隐藏中检查给定的

这是.Net 2.0,没有 LINQ。

Like the title says, I'm trying to find out if I need to include a script library that my ASP.net UserControl needs to work. I don't want to include it multiple times per page, but I want my control to be able to be used multiple times on the same page.

How can I, in the codebehind of my control, check to see if a given <script/> tag is present?

This is .Net 2.0, no LINQ.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光瘦了 2024-09-08 07:14:21
If !Page.ClientScript.IsClientScriptIncludeRegistered("jQuery")
    Page.ClientScript.RegisterClientScriptInclude("jQuery", "/scripts/jquery.js");

或者如果您需要脚本块,并且想要包含控件的类型:

if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "myScript"))
      Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myScript"
                                       , "<script>alert('xx');</script>", false);
If !Page.ClientScript.IsClientScriptIncludeRegistered("jQuery")
    Page.ClientScript.RegisterClientScriptInclude("jQuery", "/scripts/jquery.js");

or if you need a script block, And want to include the control's type:

if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "myScript"))
      Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myScript"
                                       , "<script>alert('xx');</script>", false);
稚然 2024-09-08 07:14:21

这对于您的使用来说可能有点大材小用,但我在添加到页面之前使用它来搜索现有的 CSS 和 JS:

private static bool HeaderLinkExists(Page page, string path)
{
    path = path.ToLowerInvariant();
    foreach (Control c in page.Header.Controls)
    {
        if (c is HtmlLink)
        {
            // stylesheet (or other links), check href
            HtmlLink link = (HtmlLink)c;
            if (link.Href.ToLowerInvariant().Contains(path))
            {
                return true;
            }
        }
        else if (c is HtmlGenericControl)
        {
            // any generic html tag, check for src or href
            HtmlGenericControl hgc = (HtmlGenericControl)c;
            if ((!string.IsNullOrEmpty(hgc.Attributes["src"]) && hgc.Attributes["src"].ToLowerInvariant().Contains(path)) || (!string.IsNullOrEmpty(hgc.Attributes["href"]) && hgc.Attributes["href"].ToLowerInvariant().Contains(path)))
            {
                return true;
            }
        }
        else if (c is LiteralControl)
        {
            // scripts or other html literal controls, use regex to look for src or hrefs and check each one
            LiteralControl lit = (LiteralControl)c;
            if (MatchLiteralText(lit.Text, path))
            {
                return true;
            }
        }
        else if (c is Literal)
        {
            // similar to above, use regex to look for src or hrefs and check each one
            Literal lit = (Literal)c;
            if (MatchLiteralText(lit.Text, path))
            {
                return true;
            }
        }
    }
    return false;
}

private static readonly Regex linkMatcher = new Regex(@"(?:src|href)\s*=\s*([""']?)(?<LinkValue>[^\1]+?)[\1>]", RegexOptions.Compiled);

private static bool MatchLiteralText(string text, string path)
{
    if (!string.IsNullOrEmpty(text))
    {
        text = text.ToLowerInvariant()
        foreach (Match m in linkMatcher.Matches(text))
        {
            if (m.Groups["LinkValue"].Value.Contains(path))
            {
                return true;
            }
        }
    }
    return false;
}


// usage:
if (!HeaderLinkExists(page, "/css/controlstyles.css"))
{
    HtmlHeadUtility.RegisterStylesheetInHeader(page, "~/css/controlstyles.css");
}
if (!HeaderLinkExists(page, "/js/controlscript.js"))
{
    HtmlHeadUtility.RegisterClientScriptIncludeInHeader(page, "~/js/controlscript.js");
}

如果您确定 LINKSCRIPT 保证位于 HEAD 中。

This might be a bit overkill for your usage, but I'm using it to search for existing CSS and JS before adding to the page:

private static bool HeaderLinkExists(Page page, string path)
{
    path = path.ToLowerInvariant();
    foreach (Control c in page.Header.Controls)
    {
        if (c is HtmlLink)
        {
            // stylesheet (or other links), check href
            HtmlLink link = (HtmlLink)c;
            if (link.Href.ToLowerInvariant().Contains(path))
            {
                return true;
            }
        }
        else if (c is HtmlGenericControl)
        {
            // any generic html tag, check for src or href
            HtmlGenericControl hgc = (HtmlGenericControl)c;
            if ((!string.IsNullOrEmpty(hgc.Attributes["src"]) && hgc.Attributes["src"].ToLowerInvariant().Contains(path)) || (!string.IsNullOrEmpty(hgc.Attributes["href"]) && hgc.Attributes["href"].ToLowerInvariant().Contains(path)))
            {
                return true;
            }
        }
        else if (c is LiteralControl)
        {
            // scripts or other html literal controls, use regex to look for src or hrefs and check each one
            LiteralControl lit = (LiteralControl)c;
            if (MatchLiteralText(lit.Text, path))
            {
                return true;
            }
        }
        else if (c is Literal)
        {
            // similar to above, use regex to look for src or hrefs and check each one
            Literal lit = (Literal)c;
            if (MatchLiteralText(lit.Text, path))
            {
                return true;
            }
        }
    }
    return false;
}

private static readonly Regex linkMatcher = new Regex(@"(?:src|href)\s*=\s*([""']?)(?<LinkValue>[^\1]+?)[\1>]", RegexOptions.Compiled);

private static bool MatchLiteralText(string text, string path)
{
    if (!string.IsNullOrEmpty(text))
    {
        text = text.ToLowerInvariant()
        foreach (Match m in linkMatcher.Matches(text))
        {
            if (m.Groups["LinkValue"].Value.Contains(path))
            {
                return true;
            }
        }
    }
    return false;
}


// usage:
if (!HeaderLinkExists(page, "/css/controlstyles.css"))
{
    HtmlHeadUtility.RegisterStylesheetInHeader(page, "~/css/controlstyles.css");
}
if (!HeaderLinkExists(page, "/js/controlscript.js"))
{
    HtmlHeadUtility.RegisterClientScriptIncludeInHeader(page, "~/js/controlscript.js");
}

It works great if you're sure that the LINK or SCRIPT is guaranteed to be in the HEAD.

再见回来 2024-09-08 07:14:21

不,没有。

相反,您应该维护一组已包含的

No, there isn't.

Instead, you should maintain a set of <script> tags that have already been included, and update it whenever you add a script. (For example, a [ThreadStatic] static List<String>)

蓦然回首 2024-09-08 07:14:21

我建议重新考虑你的问题/解决方案。看起来您正在尝试做的事情是将您的用户控件和使用它的页面结合起来。这需要页面引用该脚本才能使用控件。

您是否有任何理由不能将脚本标记放入用户控件中,以便使用用户控件的页面不需要知道它们需要包含特定的脚本标记才能工作?

我倾向于认为,如果您只想使用一个控件,您应该能够直接使用它。

I would suggest re-thinking your problem/solution. It seems like what you are trying to do would couple your user control and the pages that use it. This would require that a page reference that script in order to use a control.

Is there any reason you can't just put the script tag in your user control so that the pages consuming your user control don't need to know that they need to include a specific script tag to work?

I tend to think that if you want to just use a control you should be able to just use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文