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.


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>


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

<asp:Content ID="Content2" ContentPlaceHolderID="ContentHeaders" runat="server">
    <META name="keywords" content="<%=varKeywords%>">
    <META name="description" content="<%=varDescription%>">

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 = ""

Fill values in page_load event:

pageTitle = "Your Meta Tag "
varKeywords = "Your Meta Tag "
varDescription = "Your Meta Tag "

No comments: