Setting the ASP.NET Page Title and Meta Tags
page 1 of 1
Published: 26 Mar 2004
Unedited - Community Contributed
Abstract
In this article we will look at how to set the page title, meta description and meta keywords at runtime with a small about of code.
by Chris Garrett
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 83769/ 48

Setting the ASP.NET Page Title and Meta Tags

When you first look at using ASP.NET it seems that the only page objects you have access to are the ones within the FORM tags. Actually with a tiny amount of work you can set other page properties.

The two types of information are set slightly differently. The TITLE is basically a literal tag and it's value is whatever is put between the starting and ending tags.

Meta tags have their values set as attributes, so these need to be set with another approach.

The example script shows what we need to do in action. It allows the user to enter some text which resets the page title.

See it in action
/chrisg/tools/settitle.aspx

If you now take a look at the source we will see both pieces of work that need to be done.

First, in the ASPX file we need to set IDs and RUNAT attributes for each of the items we want to control.

In the codebehind file we first declare new controls for each of the items. We create them as HTMLGenericControls which seems to work fine.

To set the values we use InnerText for the Title, which sets the text that appears between its opening and closing tags, and we set the "content" attribute value for the two Meta tags.

View Source
/chrisg/tools/view-settitle.aspx.asp

/chrisg/tools/view-settitle.vb.asp

Download
/chrisg/tools/download.asp?file=settitle.aspx

/chrisg/tools/download.asp?file=settitle.vb

This is a simple demonstration, but it answers a question I get asked a fair amount. Using this, a content database and other techniques it is possible to create a workable central control for all search engine related site-tweaking :O)



User Comments

Title: My Site Ranks First In Google   
Name: Harikumar
Date: 9/24/2010 2:55:59 AM
Comment:
Hi i saw your asp code to fetch title,description and keywords from other code. it is very nice ok. but please tell me some idea. i want to fetch title,keyword from any website with help of visual basic code. Can you please send me your idea for that..
Title: limks are dead   
Name: Benjamin
Date: 8/22/2009 7:05:47 PM
Comment:
so what? The links are dead!!!
Title: links does not work   
Name: Boris
Date: 1/23/2009 9:28:30 AM
Comment:
Links doesn't work
Title: Great   
Name: Sezmo
Date: 7/5/2008 7:46:16 AM
Comment:
This is what i've been needing. Ty loads

www.reabo.co.uk
Title: inam   
Name: inamullah101@yahoo.com
Date: 4/14/2008 1:34:30 AM
Comment:
hello sir, i have an other problem,
i want to set title from a web-user control.
is there a mechanism for this?
if so, then please inform me about this.

thanks.
inam
Title: setting keyword/description atributes in asp.net 1.1   
Name: brian abraham
Date: 3/18/2008 7:02:31 PM
Comment:
Very cool - thanks! This is exactly what I was looking for. Very much appreciated
Title: in VS2005   
Name: Andrew
Date: 3/4/2008 5:54:03 AM
Comment:
In VS2005 I just do:

Me.Page.Header.Title = "My new title"

Hope this helps.
Title: Its Cool   
Name: Amar Meti
Date: 2/20/2008 1:51:52 AM
Comment:
It's a very useful article.
Thanks for it.
Title: man, i hate that   
Name: skugger
Date: 12/19/2007 12:56:50 PM
Comment:
i was happy to find out that this works, and that this is so easy to do - the "see it in action" thing does exactly what i want.

HOWEVER the examples i downloaded and put on to my own server don't do the same thing. in fact, they don't work at all. so, now, i know this can be done, and i know the answer is here on this page somewhere where i can't get to it. man, i hate that.
Title: fine   
Name: nijam
Date: 12/4/2007 1:19:14 AM
Comment:
intresting.....
Title: Mr.   
Name: Nadeem
Date: 7/31/2007 7:14:45 AM
Comment:
V.good Article and helpful for developers... see it up.. good work..
Title: Good stuff   
Name: Ralf Saha
Date: 3/12/2007 9:23:14 AM
Comment:
this helped a lot, thanks !
Title: Title for mobile page   
Name: Trishant
Date: 2/5/2007 4:02:43 AM
Comment:
Is it possible to implement this in dot net mobile web page.
Title: Good article   
Name: ismail tutumluer
Date: 1/10/2007 4:01:45 AM
Comment:
Asp.net 1.1 Meta tags literal
Title: effects on seo   
Name: fasttie
Date: 12/18/2006 6:43:11 PM
Comment:
does anyone know if this effects seo? Meaning do search engines read these tags the same as any other meta and title tags?
Title: Awesome!   
Name: adg
Date: 10/6/2006 6:23:09 PM
Comment:
I was stumped - this was exactly what I needed - Thanks!
Title: Exactly what i was looking for.   
Name: Kris
Date: 8/30/2006 4:56:29 AM
Comment:
Thanks a lot!
Title: a very useful article   
Name: Martin Stevens
Date: 6/7/2006 9:04:46 PM
Comment:
Thanks for this a very useful article, short to the point and clearly written.
Title: Thanks for the help!   
Name: Tyrone Hopes
Date: 4/13/2006 6:57:52 PM
Comment:
Nice code - pared down to the bare bones so we can see what's going on easily - and this code is just what I wanted to achieve.
Cheers!
Tyrone
Title: Yet another way to setup Meta Tags.   
Name: Mike Chaliy
Date: 3/27/2006 4:27:58 PM
Comment:
Hello.

I was develop Web Server Meta Control. Something like easy way to start with keywords and other meta tags. Hope this will help. You can found it at http://www.chaliy.com/Sources/MetaControl/.

Thanks.
Title: .Net Developer   
Name: Bala
Date: 3/27/2006 6:13:21 AM
Comment:
Fantastic code
Title: exactly what I needed thanks   
Name: Ashok kumar
Date: 2/21/2006 4:58:24 AM
Comment:
Thanks dear
really i want to this which got from ur page.

thanks once again.
Title: Thanks   
Name: Webmaster
Date: 1/26/2006 12:54:11 AM
Comment:
Thanks a bunch, was implementing this via asp literals inside the content tags but they only work when you place a space at the beginning. This solved the problem :) We are using this to set the meta tags for each photo on our free stock photo website.
Title: nice   
Name: ravi
Date: 12/6/2005 6:06:07 PM
Comment:
just what i wanted
Title: For Google   
Name: K.Chaitanya
Date: 10/5/2005 5:37:36 AM
Comment:
I hope this thing doesnot work for google spider. Because I experimented working with this. Google never takes the title name which has id as attribute. I am not sure but i need the solution to change the titles dynamically
Title: thats something whta I was looking for.....   
Name: Sithira
Date: 9/22/2005 2:19:30 AM
Comment:
Thanx.. a lot for the code... that was what i was searching for.... greate..! :D
Title: Cooool!   
Name: Pedro
Date: 9/7/2005 5:06:07 AM
Comment:
Pretty nice article.

It really helps me and I´ve got time to develop other things

Thanks!

Pedro
Title: Really good article   
Name: almo larsen
Date: 8/30/2005 5:11:32 AM
Comment:
nice and easy article, thx for that ..even if it uses VB and not C# :)
Title: this helped me in C# too   
Name: JRA
Date: 8/2/2005 12:54:11 PM
Comment:
thanks, I was having the hardest time trying to figure out how to put a variable into that title bar.
Title: Nice...   
Name: Kapil
Date: 7/11/2005 3:58:29 AM
Comment:
Good one....
Title: thanks   
Name: chris mellott
Date: 6/30/2005 9:10:07 PM
Comment:
thanks for this code; helps to dynamically inject the meta description and keywords; awesome!
Title: Oh yeah   
Name: Oscar
Date: 6/24/2005 8:11:45 AM
Comment:
Thks , i try to do this with javascrpt (LOL). This way is more simple. Thks
Title: A better way   
Name: Jeff Hastings
Date: 4/13/2005 10:04:56 AM
Comment:
I just found a solution to my problem. If you use an asp:literal between the title tags you do not have the removal of 'runat="server"' when switching from html to design view in VS.NET
Title: I have used this but for am having problems   
Name: Jeff Hastings
Date: 4/13/2005 9:50:18 AM
Comment:
I have used this before. The only problem is when you are using VS.NET to edit the pages and code and you change from design to html view or open the page the 'runat="server"' gets removed.

I am trying to find a solution but have not gotten one yet.
Title: Fantastic   
Name: Tim
Date: 4/4/2005 5:36:11 PM
Comment:
I have been musing about this all day and messing about with JavaScript! simple, clear and easy - Thanks
Title: exactly what I needed thanks   
Name: Paul Emery
Date: 11/2/2004 6:22:17 PM
Comment:
very to the point, clear and concise

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 4/23/2014 6:06:05 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search