ASP.NET: Programatically Change Page Headers (Title, Stylesheets, Meta)

Private Sub Page_Load()
    ' Change the title
    Page.Header.Title = "My Content Page Title"
   
    ' Change the background color
    Dim myStyle As New Style()
    myStyle.BackColor = System.Drawing.Color.Red
    Page.Header.StyleSheet.CreateStyleRule(myStyle, Nothing, "html")
   
    ' Create Meta Description
    Dim metaDesc As New HtmlMeta()
    metaDesc.Name = "DESCRIPTION"
    metaDesc.Content = "Content Page Meta Description"
   
    ' Create Meta Keywords
    Dim metaKeywords As New HtmlMeta()
    metaKeywords.Name = "KEYWORDS"
    metaKeywords.Content = "Content Page Meta Keywords"
   
    ' Add Meta controls to HtmlHead
    Dim head As HtmlHead = DirectCast(Page.Header, HtmlHead)
    head.Controls.Add(metaDesc)
    head.Controls.Add(metaKeywords)
End Sub

This can also be used to override MasterPage setting :)

1 comment: