Home >> ASP.NET >> Dynamic Meta Tags from SQL Database in MasterPage

Dynamic Meta Tags from SQL Database in MasterPage


Lets come back to ASP.NET. It has been long time since we blogged about ASP.NET. Earlier in this topic, we have written an article on Adding Dynamic Meta Tags (Keywords) in Blogger.

Dynamic Meta Tags from Database

As we said in title of article, this article will simplify the process of inserting meta tags in ASP.NET content pages. That can be content page or MasterPage. While creating a dynamic website with ASP.NET, most complicated task is to put meta tags in header. Here we will insert dynamic Meta tags from database in MasterPage.

As we earlier mentioned and announced, we are working on a ASP.NET based CMS which we are currently using for our website networks for multiple purpose and will make it Open Source once all of the parts are fully functional. Till far now, We have created a normal website functionality, a download center with album, artist, lyrics, download and other management features and soon we are starting a news portal.

When we create a web site with multiple pages in asp.net we can define Meta tags separately in each page, but assume when it comes to Blog or News website where, there might be hundreds or thousands of posts and each post has different content, Keywords and Description and we have to display content dynamically based on SQL queries because it is nearly impossible to generate separate page in hard disk for each posts.

We have a MasterPage to illustrate here and have saved configuration data in database. Lets get to getting them dynamically.

Database Design:

Dynamic Meta Tags

Source Code:

Now the main lines in above code are two meta tags which have runat=”server” and id also. This is our MasterPage so we have used in head section of HTML. If you are putting in content page you have to put inside ContentPlaceHolder of head area as in below code.

Source Code:

Now lets come back to backend file. We have used c# in this example.

Source Code for .cs file

This code is inside Page_Load event of page. This piece of code will get data from database which we have earlier saved in configuration. In above code, we initialized a connection, then write a T-SQL query to get all information from Configuration Table where section name is Home. Based on your database design, it might be PostID or ArticleID as parameter.

Now simply run the webpage where you put code. As we mentioned, we put code inside MasterPage, lets see the outcome.

Source View of HTML produced

Dynamic Meta Tags in ASP.NET

Here you go.

We have added Meta Description and Meta Keywords tags dynamically in our web application and these values are retrieved from database.

Thanks for reading.

About John Bhatt

Aka, Prithvi Raj Bhatt, A Proud Nepali, Blogger, Software Developer, ASP.NET, Web Designer & Developer, Faculty and a Troubleshooter blog @PRsBlog

Check Also


Print GridView only in ASP.NET

Hi, Lets come back to ASP.NET tips and trick series. We are once again going …

Leave a Reply

Your email address will not be published. Required fields are marked *