Monday, September 9, 2013

Add Meta Tag Code behind in asp.net

Introduction: In this article I will expalin how we can Add Meta Tag Code behind in asp.net.
Description:

We need to add Meta tag code behind/Dynamically in application. Suppose in application we create Content pages from Master page but we want to add different page title, Title, Description and keyword for each page.
Add a new webform or create content page from master page. On .aspx.cs page write the mention code:
protected void Page_Load(object sender, EventArgs e)
    {
        //add meta tag dynamically
        Page.Title = "Enter Page Title";

        //add meta title
        HtmlMeta metatitle = new HtmlMeta();
        metatitle.Name = "Title";
        metatitle.Content = "Enter Meta Title";
        Page.Header.Controls.Add(metatitle);

        //add meta description
        HtmlMeta metadesc = new HtmlMeta();
        metadesc.Name = "description";
        metadesc.Content = "Enter Description";
        Page.Header.Controls.Add(metadesc);

        //add meta keywords
        HtmlMeta metakeyword = new HtmlMeta();
        metakeyword.Name = "keywords";
        metakeyword.Content = "Enter Keywords";
        Page.Header.Controls.Add(metakeyword);
    }

In VB (.aspx.vb)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'add meta tag dynamically
        Page.Title = "Enter Page Title"

        'add meta title
        Dim metatitle As New HtmlMeta()
        metatitle.Name = "Title"
        metatitle.Content = "Enter Meta Title"
        Page.Header.Controls.Add(metatitle)

        'add meta description
        Dim metadesc As New HtmlMeta()
        metadesc.Name = "description"
        metadesc.Content = "Enter Description"
        Page.Header.Controls.Add(metadesc)

        'add meta keywords
        Dim metakeyword As New HtmlMeta()
        metakeyword.Name = "keywords"
        metakeyword.Content = "Enter Keywords"
        Page.Header.Controls.Add(metakeyword)

    End Sub
Run the project. To check Meta Tag right click on page>>View Page Source. See in Head tag Meta Tags are added to page.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment