Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to add Meta tags programmatically in ASP.NET

Author: Rudy Pinson
Download Source Code : 1367_MetaTagsProgrammatically.zip

In this article, I will show how you can programmatically add Meta tags.

 

Meta tags in your HTML files can be added programmatically. You can also get name and content of Meta tags from database and assign to name and content property.

 

  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Open Code behind file and write code below in Page Load method

    C#
     
    using System.Web.UI.HtmlControls;
     
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Write your page title here";
        HtmlMeta metaTag1 = new HtmlMeta();
        metaTag1.Name = "Description";
        metaTag1.Content = "Write description here";
        Page.Header.Controls.Add(metaTag1);
     
        HtmlMeta metaTag2 = new HtmlMeta();
        metaTag2.Name = "keywords";
        metaTag2.Content = "Write keywords here";
        Page.Header.Controls.Add(metaTag2);
     
        Response.Write("Meta tags added in between head tag. View page source.");
    }
     
    VB.NET
     
    Imports System.Web.UI.HtmlControls
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Page.Title = "Write your page title here";
            HtmlMeta metaTag1 = new HtmlMeta();
            metaTag1.Name = "Description";
            metaTag1.Content = "Write description here";
            Page.Header.Controls.Add(metaTag1);
     
            HtmlMeta metaTag2 = new HtmlMeta();
            metaTag2.Name = "keywords";
            metaTag2.Content = "Write keywords here";
            Page.Header.Controls.Add(metaTag2);
     
            Response.Write("Meta tags added in between head tag. View page source.");       
    End Sub
     
    Set the Title of the page. Create two HtmlMeta objects and set their Name and Content properties. Add tags to Header property.
     
  4. View the Web Site in browser and view page source
     
  5. To Get data from database write below function in code behind file

    C#
     
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
     
    public DataTable MetaTagData(int id)
    {
        string connString = ConfigurationManager.ConnectionStrings["MetaTags"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);        
        SqlCommand cmd = new SqlCommand("Select * From MetaTag Where id = @id", conn);
        cmd.Parameters.AddWithValue("id", id);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
     
    VB.NET
     
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
     
    Public Function MetaTagData(ByVal id As Integer) As DataTable
        Dim connString As String = ConfigurationManager.ConnectionStrings("MetaTags").ConnectionString
        Dim conn As New SqlConnection(connString)
        Dim cmd As New SqlCommand("Select * From MetaTag Where id = @id", conn)
        cmd.Parameters.AddWithValue("id", id)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)
        Return dt
    End Function
     
    Set your connection to your server, get data from database table and fill table.
     
  6. Comment the code written earlier in Page Load method and code below in it

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.Title = "Write your page title here";
        //HtmlMeta metaTag1 = new HtmlMeta();
        //metaTag1.Name = "Description";
        //metaTag1.Content = "Write description here";
        //Page.Header.Controls.Add(metaTag1);
     
        //HtmlMeta metaTag2 = new HtmlMeta();
        //metaTag2.Name = "keywords";
        //metaTag2.Content = "Write keywords here";
        //Page.Header.Controls.Add(metaTag2);
     
        //Response.Write("Meta tags added in between head tag. View page source.");
     
        DataTable dt = new DataTable();
        dt = MetaTagData(1);
        Page.Title = dt.Rows[0]["Title"].ToString();
        HtmlMeta metaTag1 = new HtmlMeta();
        metaTag1.Name = "Description";
        metaTag1.Content = dt.Rows[0]["Description"].ToString();
        Page.Header.Controls.Add(metaTag1);
     
        HtmlMeta metaTag2 = new HtmlMeta();
        metaTag2.Name = "keywords";
        metaTag2.Content = dt.Rows[0]["keywords"].ToString();
        Page.Header.Controls.Add(metaTag2);
     
        Response.Write("Meta tags added in between head tag. View page source.");
    }
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        'Page.Title = "Write your page title here";
        'HtmlMeta metaTag1 = new HtmlMeta();
        'metaTag1.Name = "Description";
        'metaTag1.Content = "Write description here";
        'Page.Header.Controls.Add(metaTag1);
     
        'HtmlMeta metaTag2 = new HtmlMeta();
        'metaTag2.Name = "keywords";
        'metaTag2.Content = "Write keywords here";
        'Page.Header.Controls.Add(metaTag2);
     
        'Response.Write("Meta tags added in between head tag. View page source.");
     
        Dim dt As New DataTable()
        dt = MetaTagData(1)
        Page.Title = dt.Rows(0)("Title").ToString()
        Dim metaTag1 As New HtmlMeta()
        metaTag1.Name = "Description"
        metaTag1.Content = dt.Rows(0)("Description").ToString()
        Page.Header.Controls.Add(metaTag1)
     
        Dim metaTag2 As New HtmlMeta()
        metaTag2.Name = "keywords"
        metaTag2.Content = dt.Rows(0)("keywords").ToString()
        Page.Header.Controls.Add(metaTag2)
     
        Response.Write("Meta tags added in between head tag. View page source.")
    End Sub
        
        Create objects for HtmlMeta but this time set properties using data table.
     
  7. View the Web Site in browser and view page source

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to validate XML file using XSD file in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net