Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Change Page Meta tags dynamically in asp.net using C#.

Author: Zunnair
Download Source Code : 852_ChangeMetaTags.zip

In this simple article you will learn to change title of web page dynamically.

 Meta tags are very much important for you web page, search engine use Meta tags to index you site. So some time we need to change meta tags dynamically in case of dynamic pages in asp.net.

Create a web application in c#

Drag a button and one textbox on the form

Then write code on form load event

 

C#

protected void Page_Load(object sender, EventArgs e)

    {

        HtmlHead headTag = (HtmlHead)this.Header;

 

        // Set the page title

        headTag.Title = "Title of Page";

 

        // Add a Description meta tag

        HtmlMeta PagemetaTag = new HtmlMeta();

        PagemetaTag.Name = "Description";

        PagemetaTag.Content = "Customized Description tag of the page";

        headTag.Controls.Add(PagemetaTag);

 

        // Add a Keywords meta tag

        PagemetaTag = new HtmlMeta();

        PagemetaTag.Name = "Keywords";

        PagemetaTag.Content = "Customized Keyword tag of the page";

        headTag.Controls.Add(PagemetaTag);

    }

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        Dim headTag As HtmlHead = CType(Me.Header, HtmlHead)

 

        ' Set the page title

        headTag.Title = "Title of Page"

 

        ' Add a Description meta tag

        Dim PagemetaTag As HtmlMeta = New HtmlMeta()

        PagemetaTag.Name = "Description"

        PagemetaTag.Content = "Customized Description tag of the page"

        headTag.Controls.Add(PagemetaTag)

 

        ' Add a Keywords meta tag

        PagemetaTag = New HtmlMeta()

        PagemetaTag.Name = "Keywords"

        PagemetaTag.Content = "Customized Keyword tag of the page"

        headTag.Controls.Add(PagemetaTag)

    End Sub

 

This is the simple code to change meta tags of your page dynamically.

Article Comments
Is it possible to put this function outside page_load event? Let's say I wish to change the page content when I click on the button. Is there a way to do that?

Thank you

Posted on 11/12/2009 2:24:20 PM by Patt

Hi Patt,

Yes you can paste this code any where in your code behind code on a button click event also.

Thanks

Posted on 11/13/2009 4:23:09 AM by zunnair

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Change Page title dynamically in asp.net using C#

Disclaimer - Privacy
© 2002-2017 DevASP.net