Home / ASP.NET Wiki / HTML / Programatically Change Page Headers (Title, Stylesheets, Meta)

Programatically Change Page Headers (Title, Stylesheets, Meta)

 Rate It (0)

 C#

protected void Page_Load(object sender, EventArgs e)
{
    // Change the title
    Page.Header.Title = "My Content Page Title";
   
    // Change the background color
    Style myStyle = new Style();
    myStyle.BackColor = System.Drawing.Color.Red;
    Page.Header.StyleSheet.CreateStyleRule(myStyle, null, "html");

    // Create Meta Description
    HtmlMeta metaDesc = new HtmlMeta();
    metaDesc.Name = "DESCRIPTION";
    metaDesc.Content = "Content Page Meta Description";

    // Create Meta Keywords
    HtmlMeta metaKeywords = new HtmlMeta();
    metaKeywords.Name = "KEYWORDS";
    metaKeywords.Content = "Content Page Meta Keywords";

    // Add Meta controls to HtmlHead
    HtmlHead head = Page.Header;
    head.Controls.Add(metaDesc);
    head.Controls.Add(metaKeywords);
}

VB 

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 :)

 http://nimishgarg.blogspot.com/2010/02/aspnet-programatically-changing-page.html

Revision number 3, Sunday, June 19, 2011 11:12:44 AM by horizon_net

Comments

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)