ASP.NET: Enumerating header elements from codebehind

Posted by JamesBrownIsDead on Stack Overflow See other posts from Stack Overflow or by JamesBrownIsDead
Published on 2009-11-28T00:10:54Z Indexed on 2010/04/10 1:03 UTC
Read the original article Hit count: 410

Filed under:
|
|

On Page_Load() in the codebehind, I'd like to enumerate all the <link> tags. The purpose being I want want to add a <link> to a CSS file if it isn't specified in the Page's markup.

How can I do this?

I'm thinking I should be able to use LINQ on the collection of elements in the header, no?

Here's my pseudocode:

var pageAlreadyContainsCssLink = false;

foreach(var control in this.Header.Controls) {
    if (control.TagName == "link" &&
        control.Attributes["href"] == "my_css_file.css") {
        pageAlreadyContainsCssLink = true;
        break;
    }
}

if (pageAlreadyContainsCssLink) {
    // Don't add <link> element
    return;
}

// Add the <link> to the CSS

this.AddCssLink(...);

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about css