Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Programatically Change Page Headers (Title, Stylesheets, Meta)

0.00/5 (No votes)
19 Jun 2011 1  
 C#protected void Page_Load(object sender, EventArgs e){    // Change the title    Page.Header.Title = My Content Page Title;        // Change

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here