Categories
Asp, Asp.net

Dynamic Title Keywords and Meta Description In Asp.net

Dynamic Title Keywords and Meta Description In Asp.net

In this post we will discuss how to add Dynamic Title Keywords and Meta Description In Asp.net. Dynamic Title, Meta Description and Meta Keywords tags in asp.net is very useful. Sometime on creation of dynamic page which i discussed in this post: Dynamic Page Creation In Asp.Net in asp.net lacks of title and meta tags. But by following this post we can do this too.

Here we have too approach, one is adding meta tags from database. Second is directly adding them to code.

Approach 1(Without Database):

01protected void Page_Load(object sender, EventArgs e)
02{
03// set page title
04Page.Title = "Hightechnology- Dynamic Tag";
05//Page description
06HtmlMeta pagedesc = new HtmlMeta();
07pagedesc.Name = "Description";
08pagedesc.Content ="Hightechnology- Dynamic Description";
09Header.Controls.Add(pagedesc);
10//page keywords
11HtmlMeta pagekeywords = new HtmlMeta();
12pagekeywords.Name = "keywords";
13pagekeywords.Content = "Hightechnology- Dynamic Keyword";
14Header.Controls.Add(pagekeywords);
15}

Approach 2(From Database):

01using System;
02using System.Collections.Generic;
03using System.Web;
04using System.Web.UI;
05using System.Web.UI.WebControls;
06using System.Data;
07using System.Data.SqlClient;
08using System.Configuration;
09using System.Web.UI.HtmlControls;
10 
11public partial class _Default : System.Web.UI.Page
12{
13    String strconn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
14    protected void Page_Load(object sender, EventArgs e)
15    {
16        SqlConnection con = new SqlConnection(strconn);
17        con.Open();
18        string str = "select category_title,category_keywords,category_description from category_master where category_id=1";
19        SqlCommand cmd = new SqlCommand(str, con);
20        SqlDataReader rdr = cmd.ExecuteReader();
21        if (rdr.HasRows)
22        {
23            while (rdr.Read())
24            {
25                // set page title
26                Page.Title = rdr[0].ToString();
27                //Page description
28                HtmlMeta pagedesc = new HtmlMeta();
29                pagedesc.Name = "Description";
30                pagedesc.Content = rdr[2].ToString();
31                Header.Controls.Add(pagedesc);
32                //page keywords
33                HtmlMeta pagekeywords = new HtmlMeta();
34                pagekeywords.Name = "keywords";
35                pagekeywords.Content = rdr[1].ToString();
36                Header.Controls.Add(pagekeywords);
37 
38            }
39        }
40con.Close();
41    }
42}