I saw a good solution on a foreigner's website, so I simply compiled it:
In an ASP.NET application, it is often necessary to dynamically modify the title of the page. A typical example is that in a page navigation control, which link is expected to be clicked by the user, the relevant content will be displayed in the title of the page. For example, a website has the following website structure:
If there are book categories, and then there are Chinese books and foreign book categories, you can generally use the tree shape or the new navigation bar control of asp.net 2.0.
(sitemap), to achieve, such as
books--->Chinese books;
Books---->Foreign books, etc., and if at this time, it can also display such as "Books-->Chinese books" in the <title> part of the page, it would be more intuitive and obvious.
In asp.net 2.0, we can use the server-side control in the <head> part to achieve this. First, we need to add tags
<head runat="server">
Then you can change the title content in the page_load event in the following form, such as
Page.Header.Title = "The current time is: " & DateTime.Now.ToString()
, or can be simply written as page.title.
Then, we can combine it with the sitemap control in this way. The implementation method is as follows:
Const DEFAULT_UNNAMED_PAGE_TITLE As String = "Untitled Page"
Const DEFAULT_PAGE_TITLE As String = "Welcome to my Website!!"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set the page's title, if needed
If String.IsNullOrEmpty(Page.Title) OrElse Page.Title = DEFAULT_UNNAMED_PAGE_TITLE Then
If SiteMap.CurrentNode Is Nothing Then
Page.Title = DEFAULT_PAGE_TITLE
Else
Page.Title = GetPageTitleBasedOnSiteNavigation()
'Can also use the following if you'd rather
'Page.Title = GetPageTitleBasedOnSiteNavigationUsingRecursion(SiteMap.CurrentNode)
End If
End If
End Sub
Private Function GetPageTitleBasedOnSiteNavigation() As String
If SiteMap.CurrentNode Is Nothing Then
Throw New ArgumentException("currentNode cannot be Nothing")
End If
'We are visiting a page defined in the site map - build up the page title
'based on the site map node's place in the hierarchy
Dim output As String = String.Empty
Dim currentNode As SiteMapNode = SiteMap.CurrentNode
While currentNode IsNot Nothing
If output.Length > 0 Then
output = currentNode.Title & " :: " & output
Else
output = currentNode.Title
End If
currentNode = currentNode.ParentNode
End While
Return output
End Function
first predefines two constants, and then gradually establishes the node of the sitemap. At the beginning, the node is null, and then calls
GetPageTitleBasedOnSiteNavigation() This process can be implemented by connecting each sitemap node with a string and finally returning it to page.title. Of course, it can also be implemented recursively.
Source: http://jackyrong.cnblogs.com/archive/2006/05/15/400345.html