Home Asp.net Add aspx Page Title, MetaKeywords, MetaDescription Tags Dynamically

Add aspx Page Title, MetaKeywords, MetaDescription Tags Dynamically

618
0

In my previous tutorials, I’d explained asp.net menu control example, how to show alert message from code-behind, how to call javascript function from code-behind and other more cracking tutorials on Asp.net, JavaScript and jQuery here.

Now here in this tutorial, I’ll explain how you can add aspx page title, metakeywords, metadescription tags dynamically from code-behind in web pages using asp.net c# or vb.net with example demo code.

Asp.net .aspx page title and meta tags like keywords and description represents a metadata and also useful for page rankings in SEO. If you want to add meta tags in your asp.net project or website, then follow these easy steps.

Note: If you have existing project or website, ignore first two steps.

  • Create new website or project
  • To test example, create one Default.aspx page OR you can also use your existing web pages

Once your Default.aspx page is added to your project or website, add the following code to your .aspx page as shown below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h4>Add Page Title, Meta Tags like Keyword and Description from code-behind 
                Dynamically in Asp.net using C# or Vb.net</h4>
        </div>
    </form>
</body>
</html>

Now you need to add the following code to your code-behind file to add meta tags dynamically. Choose your favorite programming language (that is C# or Vb.net) and copy-paste following code on “Page_Load” event.

Add aspx Page Title, MetaKeywords, MetaDescription Tags Dynamically In C#

protected void Page_Load(object sender, EventArgs e) {
    //Adding Page Title
    Page.Title = @"Add Page Title, Meta Tags like Keyword and Description from
                    code-behind Dynamically in Asp.net using C# or Vb.net";
        
    //Adding Page MetaKeywords
    Page.MetaKeywords = "page, title, meta, tags, keyword, description, asp.net";

    //Adding Page Description
    Page.MetaDescription = @"Here in this tutorial, I'll explain how to add meta tags
                                dynamically from code-behind in asp.net using c# or vb.net 
                                with sample demo code";
}

Add aspx Page Title, MetaKeywords, MetaDescription Tags Dynamically In Vb.net – [.vb]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    'Adding Page Title
    Page.Title = "Add Page Title, Meta Tags like Keyword and " & ControlChars.CrLf &
    "Description from code-behind Dynamically in Asp.net using C# or Vb.net"
    'Adding Page MetaKeywords
    Page.MetaKeywords = "page, title, meta, tags, keyword, description, asp.net"
    'Adding Page Description
    Page.MetaDescription = "Here I'll explain how to add " & ControlChars.CrLf &
    "meta tags dynamically from code-behind in asp.net using c#/vb.net with demo"
End Sub

You can also set page title, keywords, description directly from Default.aspx page statically. If you want so, here is the sample code snippet to add meta tags statically.

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Add Page Title, Meta Tags like Keyword and Description from code-behind
            Dynamically in Asp.net using C# or Vb.net</title>
    <meta name="keywords" content="page, title, meta, tags, keyword, description" />
    <meta name="description" content="Here in this tutorial, I'll explain how to add
            meta tags dynamically from code-behind in asp.net with sample demo code" />
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h4>Add Page Title, Meta Tags like Keyword and Description from code-behind
                Dynamically in Asp.net using C# or Vb.net</h4>
        </div>
    </form>
</body>
</html>
Note: These settings are invisible to user and falls under HTML head tag elements, whereas HTML body tag elements are visible to users. To check the result, you need to run Default.aspx page and then press “Ctrl” + “u” key OR right click on browser and select “View Page Source” or “Page Source” depending on browser settings.

Example Result

Add aspx Page Title, MetaKeywords, MetaDescription Tags Dynamically
Add aspx Page Title, MetaKeywords, MetaDescription Tags Dynamically
Previous articleAsp.Net Menu Control Example
Next articleGet GridView Selected Row HiddenField Values in jQuery From Client-side
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

eleven − 5 =