Adding Page Title, Meta Tags dynamically in ASP.NET 3.5 C#

ASP.NET 4 Comments on Adding Page Title, Meta Tags dynamically in ASP.NET 3.5 C#

For Search Engine Optimization you have to add the page title and the meta tags in the all the pages. If it is a static page you can just add in the head section of the page. But in a dynamic asp.net page you have to dynamically generate the meta tag with the relevant information and deliver in the asp.net page.

This method utilizes the

System.Web.UI.HtmlControls namespace.

The System.Web.UI.HtmlControls namespace contains classes that allow you to create HTML tags on your .aspx web page or web form ( remember web form is the .aspx page and .cs or .vb is the code behind file). Let us start

Create a new website in your VWD EXPRESS and add a new webform, let us say default.aspx. We are not going to add anything in this page. While viewing this page in the browser we should have the necessary page title and meta tags for our SEO.

Open the code behind file of the default.aspx page and the add the
[sc:tcbox ]using System.Web.UI.HtmlControls;[sc:/tcbox ]
namespace in the top.

Now in the the Page_Load event add

[sc:tcbox ]Page.Title = "This is the page title"[sc:/tcbox ]

This adds the title tag and title of your web page.

next to add the meta tags we will declare and instantiate two meta tag elements

[sc:tcbox ]// Create two instances of an HtmlMeta control.

HtmlMeta hm1 = new HtmlMeta();

HtmlMeta hm2 = new HtmlMeta();[sc:/tcbox ]

Next add the name and the contents of this meta tags

[sc:tcbox ]// Define an HTML <meta> element – description – that is useful for search engines.
hm1.Name = “description”;
hm1.Content = “this is the description text of the meta tag”;

// Define an HTML <meta> element – keywords – that is useful for search engines.
hm2.Name = “keywords”;
hm2.Content = “these are the keywords of the meta tag”;[sc:/tcbox ]

Finally get reference to the page header element and include these meta tag elements in the header.

[sc:tcbox ]// Get a reference to the page header element.
HtmlHead head = (HtmlHead)Page.Header;

head.Controls.Add(hm1);

head.Controls.Add(hm2);[sc:/tcbox ]

When we load the default.aspx page it should be populated with title, meta tag description, meta tag keywords.

Code Sample

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
 <title></title>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>
 Meta tags added to this page , through code behind. Verify with Edit&gt;View Source
 in IE.</div>
 </form>
 </body>
</html>

Default.aspx.cs


using System;
 using System.Web.UI;
 using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
 {

protected void Page_Load(object sender, EventArgs e)
 {
 Page.Title = "This is the page title";

// Create two instances of an HtmlMeta control.

HtmlMeta hm1 = new HtmlMeta();

HtmlMeta hm2 = new HtmlMeta();

// Define an HTML <meta> element - description - that is useful for search engines.
 hm1.Name = "description";
 hm1.Content = "this is the description text of the meta tag";

// Define an HTML <meta> element - keywords - that is useful for search engines.
 hm2.Name = "keywords";
 hm2.Content = "these are the keywords of the meta tag";

// Get a reference to the page header element.
 HtmlHead head = (HtmlHead)Page.Header;

head.Controls.Add(hm1);

head.Controls.Add(hm2);

}
}

/span



Author

Search

Back to Top