开发者

How can I tell if a <script> tag with a given src attribute is present on the page in ASP.net?

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 开发者_如何学Cpresent?

This is .Net 2.0, no LINQ.


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);


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.


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>)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜