Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

I came across a blog entry by Rich Strahl about adding stylesheets to ASP.NET 2.0 web pages. The way Rick explained it is that he'd recommend a contentplaceholder in the header section of a masterpage and to set the properties there. That's just fine and dandy if the content you'd like to set is in a Masterpage to Page setup. However, you can easily set the header information programatically in 2.0 by accessing the new Header class. Here is an example of how to do this:

Dim KeywordsHtmlMeta as new HtmlMeta

KeywordsHtmlMeta.Name = "Keywords"
KeywordsHtmlMeta.Content = "ASP.NET, Blog, Jason, Gaylord"
Page.Header.Controls.Add(KeywordsHtmlMeta)

Dim CSSHtmlLink as new HtmlLink

CSSHtmlLink.href="~/style.css"
CSSHtmlLink.Attributes.Add("rel", "Stylesheet")
CSSHtmlLink.Attributes.Add("type", "text/css")
CSSHtmlLink.Attributes.Add("media", "all")
Page.Header.Controls.Add(CSSHtmlLink)
Published Tuesday, May 30, 2006 11:04 AM by Jason N. Gaylord

Comments

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Thursday, February 15, 2007 8:53 AM by munklefish

Thanks for the info!

Saved me a lot of time.

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Thursday, April 05, 2007 3:15 PM by twd76

Thanks for the post ... saved me quite a bit of time!

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Friday, August 10, 2007 9:21 AM by Nalaka

Is there a way to create meta tags like this and save those meta tags in the page header permanently ?

Other wise we have to save the meta tag(meta tag name & content) somewhere(in a database, xml file, etc..) and bind to page header in the page load. Isn't it?

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Tuesday, December 25, 2007 10:00 PM by Alejandro Barrada Martin

Hello, I write a full post for all of those who want to write headers meta tags trought asp.net code.

It's written in Spanish but it's very useful: http://abmartin.wordpress.com

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Thursday, January 03, 2008 11:41 AM by riddhi

thanks allot. saves a lot of time and makes it seo friendly

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Wednesday, May 28, 2008 4:15 AM by caprisha

Thanks for the post ... saved a lot of time!, i write a post   for adding meta tags in asp.net 2.0 using a base class that inherits from System.Web.UI.Page and contains properties for meta tags for content pages....

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Monday, June 30, 2008 7:25 AM by Santosh Kumar

Thanks for giveing exclent article. this article save more time for adding meta tag and style sheet.

Thanks

www.operativesystems.com

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Saturday, November 29, 2008 7:46 AM by m1gin

Hi,

This is useful...

But because I used <%=Something %> code block in header, I getting error like

"The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). "

So, I am searched to another method.

I found one... But, this can set new value to already exist tags.

But I don't sure if this is a good method because the function each time searching for the wanted meta tag.

---------------------------------

Public Shared Sub MetaTag(ByVal thisPage As Page, ByVal name As String, ByVal content As String)

Dim meta As New HtmlMeta

' Loop through all the controls in the Header

For Each ctrl As Control In thisPage.Header.Controls

' Check if the control is a Meta Tag

If TypeOf ctrl Is HtmlMeta Then

' Get the Meta Tag and check if the name is the same

meta = DirectCast(ctrl, HtmlMeta)

If name.Equals(meta.Name, StringComparison.InvariantCultureIgnoreCase) Then

' Since the Meta Tag already exists, simply update and exit

meta.Content = content

Return

End If

End If

Next

End Sub

# re: Adding Meta Tags and Stylesheets to ASP.NET 2.0 Pages Programatically

Wednesday, June 10, 2009 6:12 AM by Rakesh Thakor - Vipul Limbachiya

Thanks, its really useful.

Leave a Comment

(required) 
(required) 
(optional)
(required)