In this article I will explain with an example, how to add Meta Tags in Master Page, Child Page (ASPX Content Page).
The Title and Meta Tags like Keywords from and Description are dynamically populated from database and programmatically added to page head section of the Master Page in ASP.Net.
Database
I have made use of the following table MetaTags with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Download SQL file
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.HtmlControls
Adding Meta tags to Master Page and ASPX Child Content Pages from Database in ASP.Net
Inside the Page Load event, first the name of the Current Page is determined from the Request.Url.Segments property.
Once the name of the Current Page is determined, the Title, Keywords and the Description values are fetched from the Database Table.
Finally the Title, Keywords and the Description are added to the Page Head section using the HtmlMeta class objects.
C#
protected void Page_Load(object sender, EventArgs e)
{
string page = Request.Url.Segments[Request.Url.Segments.Length - 1];
DataTable dtMeta = this.GetData(page);
//Add Page Title.
this.Page.Title = dtMeta.Rows[0]["Title"].ToString();
//Add Keywords Meta Tag.
HtmlMeta keywords = new HtmlMeta();
keywords.HttpEquiv = "keywords";
keywords.Name = "keywords";
keywords.Content = dtMeta.Rows[0]["Keywords"].ToString();
this.Page.Header.Controls.Add(keywords);
//Add Description Meta Tag.
HtmlMeta description = new HtmlMeta();
description.HttpEquiv = "description";
description.Name = "description";
description.Content = dtMeta.Rows[0]["Description"].ToString();
this.Page.Header.Controls.Add(description);
}
private DataTable GetData(string page)
{
string query = "SELECT Title, Description, Keywords FROM MetaTags WHERE LOWER(Page) = LOWER(@Page)";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Page", page);
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim page As String = Request.Url.Segments(Request.Url.Segments.Length - 1)
Dim dtMeta As DataTable = Me.GetData(page)
'Add Page Title.
Me.Page.Title = dtMeta.Rows(0)("Title").ToString()
'Add Keywords Meta Tag.
Dim keywords As New HtmlMeta()
keywords.HttpEquiv = "keywords"
keywords.Name = "keywords"
keywords.Content = dtMeta.Rows(0)("Keywords").ToString()
Me.Page.Header.Controls.Add(keywords)
'Add Description Meta Tag.
Dim description As New HtmlMeta()
description.HttpEquiv = "description"
description.Name = "description"
description.Content = dtMeta.Rows(0)("Description").ToString()
Me.Page.Header.Controls.Add(description)
End Sub
Private Function GetData(page As String) As DataTable
Dim query As String = "SELECT Title, Description, Keywords FROM MetaTags WHERE LOWER(Page) = LOWER(@Page)"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Page", page)
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot
Downloads