Add meta tags dynamically to a page having a master page in Asp.Net


As a master page provides a common layout to all pages, and <head> tag is included in the master page itself instead of content page.  So one questing comes how to add different meta tags to all different pages (or dynamically generate meat tags) .

This post describes adding a meta tag in an dynamic page with dynamically generated content. This code can be used if you want to add meta tag to a page which is using Master pages.

Two approaches can be taken for this, one is using .net framework’s features, and second is by playing with “ContentPlaceHold”.

Approach 1 : Using .net framework’s features to add meta tags

Add the code below in page_load event of the content page.

Dim metaDescription As New HtmlMeta()
metaDescription.Name = “metaTagHeading (example: keywords, description)”
metaDescription.Content = “A description or content of meta tag here”
Me.Header.Controls.Add(metaDescription)


Approach 2 : Playing with “ContentPlaceHold”.

Step1

Put one content place holder in your master page in HEAD section, like::

<head id=”Head1″ runat=”server”>
<asp:ContentPlaceHolder runat=”server” id=”ContentHeaders”></asp:ContentPlaceHolder>
</head>

Step2

Now in the content (child) page, put this code:

<asp:Content ID=”Content2″ ContentPlaceHolderID=”ContentHeaders” runat=”server”>
<TITLE><%=pageTitle%></TITLE>
<META name=”keywords” content=”<%=varKeywords%>”>
<META name=”description” content=”<%=varDescription%>”>
</asp:Content>

Step3
In content page’s vb or cs file code:

Declare variables as below in the global declaration section (not in page load)

Protected pageTitle As String = “”
Protected varKeywords As String = “”
Protected varDescription As String = “”

Step4
Fill values in page_load event:

pageTitle = “Your Meta Tag ”
varKeywords = “Your Meta Tag ”
varDescription = “Your Meta Tag ”

**End of article**

For more articles updates and sourcecodes please visit dotnetcoderoom

Advertisements

About dotnetcoderoom

Working as an Onsite coordinator in USA. Handling UAT, CR (Change Requests), Product Support/Development, Build Management. Supervise and assist in live product implementation (at client side) and training. Providing technical direction for the development, design, and system integration. Closely monitoring and working with offshore team for Enhancements, Bug Fixing, Enforce coding standards, perform code reviews and mentors junior developers. Proven track of timely delivery of enterprise level web based applications. Skilled in a variety of software languages, tools, and methodologies, with a special expertise in Microsoft .NET technologies. MCTS .Net 4.0 Web applications MCTS .Net 2.0 Web applications GNIIT, MCA, B.Com Specialties 8+ Years of experience in .NET Technologies. Expertise in .NET, ASP.NET, MVC, C#, VB.NET, WCF, Web Services, MS SQL, JQuery. Excellent R&D and troubleshooting skills. Extensive knowledge in E-Commerce and Healthcare domain. View all posts by dotnetcoderoom

4 responses to “Add meta tags dynamically to a page having a master page in Asp.Net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: