Introduction
We can programmatically change the Title or Cascading Style Sheet rules included
in a Master Page, and then we can use the Page.Header property. This property
returns an object that implements the IPageHeader interface. This interface has
the following two properties:
For example, the page given below uses the
SimpleMaster.master Master Page. It changes the Title and background color of
the Master Page.
Default.aspx.vb File Code
Partial
Class
_Default
Inherits System.Web.UI.Page
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs)
Handles Me.Load
' Changing title of the Page
Page.Header.Title = String.Format("Header
Content ({0})", DateTime.Now)
'
Changing background of Page
Dim myStyle
As New
Style()
myStyle.BackColor = System.Drawing.Color.Red
Page.Header.StyleSheet.CreateStyleRule(myStyle,
Nothing,
"html")
'
Creating Meta Description
Dim metaDesc
As New
HtmlMeta()
metaDesc.Name = "DESCRIPTION"
metaDesc.Content = "A
sample of using HtmlMeta controls"
'
Creating Meta Keywords
Dim metaKeywords
As New
HtmlMeta()
metaKeywords.Name = "KEYWORDS"
metaKeywords.Content = "HtmlMeta,Page.Header,ASP.NET"
' Adding
Meta controls to HtmlHead
Dim head
As
HtmlHead = CType(Page.Header,
HtmlHead)
head.Controls.Add(metaDesc)
head.Controls.Add(metaKeywords)
End Sub
End
Class
Default.aspx File Code
<%@
Page Title=""
Language="VB"
MasterPageFile="~/MasterPage.master"
AutoEventWireup="false"
CodeFile="Default.aspx.vb"
Inherits="_Default"
%>
<asp:Content
ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
This is my content.
<br
/>This is my content.
<br
/>This is my content.
<br
/>This is my content.
<br
/>This is my content.
</asp:Content>
In above code Page.Header property will return the server-side<head> tag
contained in the Master Page. We can cast the object returned by this property
to an HTMLHead control. For example, in above page modifies the Master Page
<meta> tags. Tags are used by search engines when indexes the page. So, if we
need to get higher rank in search then have to carefully write the tags.
Remember that the Page_Load() method in above example creates two HtmlMeta
controls. The first control represents a Meta Description tag and the second
control represents a Meta Keywords tag. Both HtmlMeta controls are added to the
HtmlHead control's Controls collection. When the page is rendered, the following
tags are added to the <head> tag:
'
Creating Meta Description
Dim metaDesc
As New
HtmlMeta()
metaDesc.Name = "DESCRIPTION"
metaDesc.Content = "A
sample of using HtmlMeta controls"
'
Creating Meta Keywords
Dim metaKeywords
As New
HtmlMeta()
metaKeywords.Name = "KEYWORDS"
metaKeywords.Content =
"HtmlMeta,Page.Header,ASP.NET"
' Adding
Meta controls to HtmlHead
Dim head
As
HtmlHead = CType(Page.Header,
HtmlHead)
head.Controls.Add(metaDesc)
head.Controls.Add(metaKeywords)
HAVE A GREAT CODING!