ASP.NET: Enumerating header elements from codebehind
- by JamesBrownIsDead
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(...);