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):

protected void Page_Load(object sender, EventArgs e)
{
// set page title
Page.Title = "Hightechnology- Dynamic Tag";
//Page description
HtmlMeta pagedesc = new HtmlMeta();
pagedesc.Name = "Description";
pagedesc.Content ="Hightechnology- Dynamic Description";
Header.Controls.Add(pagedesc);
//page keywords
HtmlMeta pagekeywords = new HtmlMeta();
pagekeywords.Name = "keywords";
pagekeywords.Content = "Hightechnology- Dynamic Keyword";
Header.Controls.Add(pagekeywords);
}

Approach 2(From Database):

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    String strconn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(strconn);
        con.Open();
        string str = "select category_title,category_keywords,category_description from category_master where category_id=1";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                // set page title
                Page.Title = rdr[0].ToString();
                //Page description
                HtmlMeta pagedesc = new HtmlMeta();
                pagedesc.Name = "Description";
                pagedesc.Content = rdr[2].ToString();
                Header.Controls.Add(pagedesc);
                //page keywords
                HtmlMeta pagekeywords = new HtmlMeta();
                pagekeywords.Name = "keywords";
                pagekeywords.Content = rdr[1].ToString();
                Header.Controls.Add(pagekeywords);

            }
        }
con.Close();
    }
}