German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


How to Create Meta Tags Programmatically in ASP.NET 2.0

Written by khattak2 on Nov 30, 2006
This Article will explain how can one create the Page meta tag Dynamically.

Introducation:

In ASP.NET, we have an option to add head tags in HTML where we could define our Meta tags like keyword and description but in ASP.NET 2.0 if we use master page inheritated page then there is problem to add meta tags. This article explains how you can add meta tags in ASP.NET 2.0.

Explanation:

If you have been Developing Web, you may be familiar with the meta tags in an HTML page. The meta tags are used to provides keywords etc in an HTML page.

 

Now in ASP.NET 2.0, you can add these meta tags programmatically. The HtmlMeta class provides programmatic access to the HTML element on the server. The HTML element is a container for data about the rendered page, but not page content itself.

 

The Name property of HtmlMeta provides the property name and the Content property is used to specify the property value. The Scheme property to specify additional information to user agents on how to interpret the metadata property and the HttpEquiv property in place of the Name property when the resulting metadata property will be retrieved using HTTP.


The following code shows how to add meta tags to a page programmatically.


VB Version:

Private Sub CreateMetaTags()  
    Dim hm As New HtmlMeta()
    Dim head As HtmlHead = CType(Page.Header, HtmlHead)
     hm.Name = "Keywords"
    hm.Content = "VB.Net, VB.NET, .NET"
    head.Controls.Add(hm)
End Sub 'CreateMetaTags


C# Version:

HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = "Keywords";
metaTag.Content = "Dotnet-Friends.com, Dotnet-Friends,.Net, C#, CSharp";
this.Header.Controls.Add(metaTag);


Add the above created method to Load event of the Page. Now The Result can be seen by viewing dynamically cretaed source of the Page.


Test the Code:

In the IE go to View > Source


Similarly, you can add multiple meta tags to the header of the Page. You can also add sechema to the Contact.

// Render:
meta = new HtmlMeta();
meta.Name = "date";
meta.Content = DateTime.Now.ToString("yyyy-MM-dd");
meta.Scheme = "YYYY-MM-DD";
this.Header.Controls.Add(meta);

 

Visitors/Readers Comments



how to add meta tags to another html page from code??

Comments By:sum

at: 14/06/2007 05:53:22 UTC

There is only one senario for inserting/adding a tag in to an other page and i.e when a new page is beeing cerated. In other words, cross-page-code execution is only allowed/possible at the page creation/initialization.

For detail answers please use the Forum.

Comments By:kamal

at: 14/06/2007 10:16:48 UTC
Thank you for this code. It really helped me for my project, specially for SEO.
Regardes
Jay

Comments By:Jay - MyPhotoSelection.com

at: 20/07/2007 12:33:03 UTC
Very Very Good.

Comments By:Mayur

at: 07/12/2007 03:56:00 UTC



Add your Comments

Name:
 
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)