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>
Example Result
