The HtmlTitle and HtmlMeta classes, new in ASP.NET 2.0, provide a simpler way to programmatically manipulate the HTML <head> tag in web forms applications.
Use the HtmlTitle class to programmatically specify the HTML title element of a Web Forms page.
The HtmlMeta control provides programmatic access to the HTML meta element on the server. The HTML meta element is a container for data about the rendered page, but not page content itself. The meta tag is placed between the opening and closing HTML head elements. Each meta element describes a metadata property name and its associated value.
Code Example
Partial Class _Default
Inherits Page
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetWebHead()
End Sub
Protected Sub SetWebHead()
If (Page.Header IsNot Nothing) Then
' At this point data could be retrieved - from a database for example -
' for the purpose of dynamically manipulating the page <Head> element.
' To keep this example simple we will pretend to retrieve the data.
' Pretend to retrieve a page title from a PageTitle column in a database table.
Dim pageTitle As String = "Programmatically Manipulate Web Page Head Element"
' Pretend to retrieve page keywords from a Keywords column in a database table.
Dim pageKeywords As String = "Visual Basic,web head"
' Pretend to retrieve a page descripton from a PageDescription column in a database table.
Dim pageDescription As String = "Programatically manipulate the <Head> element in a Visual Basic Web Page"
' Pretend to retrieve a page date from a PageDate column in a database table.
Dim pageDate As String = "2006-08-13"
' Apply the 'retrived' data to the page head element.
' Set the page title.
Page.Header.Title = pageTitle
' Add an HTML <meta> element that defines keywords for the page.
Dim keywordsHtmlMeta As New HtmlMeta()
keywordsHtmlMeta.Name = "keywords"
keywordsHtmlMeta.Content = pageKeywords
' Add the HtmlMeta object to the page header's controls.
Page.Header.Controls.Add(keywordsHtmlMeta)
' Add an HTML <meta> element that describes the page.
Dim descriptionHtmlMeta As New HtmlMeta()
descriptionHtmlMeta.Name = "description"
descriptionHtmlMeta.Content = pageDescription
' Add the HtmlMeta object to the page header's controls.
Page.Header.Controls.Add(descriptionHtmlMeta)
' Add an HTML <meta> element that dates the page.
Dim dataHtmlMeta As New HtmlMeta()
dataHtmlMeta.Name = "date"
dataHtmlMeta.Content = pageDate
' The Scheme property allows you to specify a scheme attribute
' of the rendered HTML <meta> element. The scheme attribute can
' be used to provide user agents, such as client browsers or search
' engines, additional context for interpreting the metadata property.
dataHtmlMeta.Scheme = "YYYY-MM-DD"
' Add the HtmlMeta object to the page header's controls.
Page.Header.Controls.Add(dataHtmlMeta)
End If
End Sub
End Class
Click the link beloew to download Visual Basic source code in a Visual Studio 2005 ASP.NET 2.0 solution which demonstrates how to use the HtmlTitle and HtmlMeta classes to manipulate the HTML head tag.