Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Add Meta tags to Asp.Net Master Page and ASPX Child Content Pages

May 24, 2015
Introduction

Here I will explain how to add meta tags in master and child pages in
asp.net using c#, vb.net or add meta tags like page title, description, keywords dynamically in asp.net using c#, vb.net or dynamically change page title and description in master page and child content page in asp.net using c#, vb.net.


Before implement this example first design one table pagetitledescription in your database like as shown below

Column Name
Data Type
Allow Nulls
Pageid
Int(IDENTITY=TRUE)
No
Pagename
varchar(50)
Yes
Pagetitle
varchar(50)
Yes
pagekeywords
varchar(50)
Yes
pagedescription
varchar(250)
Yes
Once we design table in our database that would be like as shown below

Now insert some dummy data in your table to use it in our application. Now open your aspx page and write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Aspdotnet-Suresh.com Meta Tags Example</b>
</div>
</form>
</body>
</html>

Now add following namespaces in code behind

C# Code


using System;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;

After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = GetPageDetails("samplepage");
// set page title
if (dt.Rows.Count > 0)
{
this.Page.Title = dt.Rows[0]["pagetitle"].ToString();
//page keywords
HtmlMeta pagekeywords = new HtmlMeta();
pagekeywords.Name = "keywords";
pagekeywords.Content = dt.Rows[0]["pagekeywords"].ToString();
Header.Controls.Add(pagekeywords);
//Page description
HtmlMeta pagedesc = new HtmlMeta();
pagedesc.Name = "Description";
pagedesc.Content = dt.Rows[0]["pagedescription"].ToString();
Header.Controls.Add(pagedesc);
}
}
private DataTable GetPageDetails(string pagename)
{
DataTable dtresult = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from pagetitledescription where pagename=@pagename", con);
cmd.Parameters.AddWithValue("@pagename",pagename);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtresult);
}
return dtresult;
}

VB.NET Code


Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.SqlClient

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = GetPageDetails("samplepage")
' set page title
If dt.Rows.Count > 0 Then
Me.Page.Title = dt.Rows(0)("pagetitle").ToString()
'page keywords
Dim pagekeywords As New HtmlMeta()
pagekeywords.Name = "keywords"
pagekeywords.Content = dt.Rows(0)("pagekeywords").ToString()
Header.Controls.Add(pagekeywords)
'Page description
Dim pagedesc As New HtmlMeta()
pagedesc.Name = "Description"
pagedesc.Content = dt.Rows(0)("pagedescription").ToString()
Header.Controls.Add(pagedesc)
End If
End Sub
Private Function GetPageDetails(ByVal pagename As String) As DataTable
Dim dtresult As New DataTable()
Using con As New SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select * from pagetitledescription where pagename=@pagename", con)
cmd.Parameters.AddWithValue("@pagename", pagename)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dtresult)
End Using
Return dtresult
End Function
End Class

Demo

Add Meta tags to Asp.Net Master Page and ASPX Child Content Pages


Download Sample Code Attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.