Google Search

Google
 

Thursday, December 6, 2007

Dynamically Adding Meta Tags in ASP.NET 2.0

Custom Adding Title Tag :
Me.Header.Title = "Title Of Page Here"

Custom Adding Meta Tag :
Dim metaDescription As New HtmlMeta()
metaDescription.Name = "description"
metaDescription.Content = "A description of the page here."
Me.Header.Controls.Add(metaDescription)

Custom Adding Style :
Dim styles As New HtmlGenericControl("style")
styles.Attributes.Add("type", "text/css")
styles.InnerText = "p { font-weight: bold; }"
Me.Header.Controls.Add(styles)

Dim style As New Style()
style.ForeColor = System.Drawing.Color.Navy
style.BackColor = System.Drawing.Color.LightGray
Me.Header.StyleSheet.CreateStyleRule(style, Nothing, "body")

Custom Adding Css Style:
Dim cssLink As New HtmlLink()
cssLink.Href = "styles.css"
cssLink.Attributes.Add("rel", "Stylesheet")
cssLink.Attributes.Add("type", "text/css")
Me.Header.Controls.Add(cssLink)

Custom Adding Java Script:
Dim javaScript As New HtmlGenericControl("script")
javaScript.Attributes.Add("type", "text/javascript")
javaScript.InnerText = "alert('Hello World!');"
Me.Header.Controls.Add(javaScript)

All the above code should be used within page Page_Load event