Click here to Skip to main content
15,885,309 members
Articles / null
Article

Programatically Change Page Headers (Title, Stylesheets, Meta)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 18.2K   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
QuestionDouble meta tags - it is not changed but added Pin
Member 16333942-Mar-16 3:48
Member 16333942-Mar-16 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.