Saturday, June 21, 2008

Add meta tags to master page from content page Asp.Net

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.

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 "

No comments: