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.
Description:
In previous articles I explained jQuery crop and upload image with preview in asp.net, jQuery upload multiple files in asp.net using handler, jQuery load more data in repeater on button click in asp.net, jQuery show asp.net exception message in ajax calls, cascading dropdownlist in gridview using asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to add metags in master page and child pages in asp.net using c#, vb.net.
In previous articles I explained jQuery crop and upload image with preview in asp.net, jQuery upload multiple files in asp.net using handler, jQuery load more data in repeater on button click in asp.net, jQuery show asp.net exception message in ajax calls, cascading dropdownlist in gridview using asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to add metags in master page and child pages 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
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. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.