Dynamically modify page title in c#2.0

Source: Internet
Author: User
Tags current time implement modify
Dynamic | page

In the foreigner's station sees solves the good method, therefore simple compiles:
In a asp.net application, often to dynamically modify the title of the page, a typical example is, in a page navigation control, want to select which connection users, in the title of the page to display the relevant content, for example, a website, like the following Web site architecture:
There are books classification, the following there are Chinese books, foreign books classification, you can generally use the tree or ASP.net 2.0 of the newly added navigation bar control
(sitemap), to implement, for example

Book---> Chinese books;
Books----> Foreign books
And so on, and if this time, can be in the page <title> part, also can show such as "book--> Chinese book" So, that more intuitive obvious,
In asp.net 2.0, we can use the

You can then change the title of the Page_Load event in the following form, such as
Page.Header.Title = "The current time is:" & DateTime.Now.ToString ()
, you can simply write Page.title.

We can then combine it with the Sitemap control in such a way that it is implemented 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) H Andles me.load
        ' Set the page ' s title, if needed
         If string.isnullorempty (page.title) OrElse page.title = Default_unnamed_page_title Then
& nbsp;           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 is nothing")
End If

' We are visiting a page defined in the site Map-build up the page title
' Based on the site map node ' 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

In the above code, first predefined two constants, and then gradually set up the Sitemap node, the first node is null, and then call the
Getpagetitlebasedonsitenavigation () This process, in each building a sitemap node, with a string to connect, and finally return to Page.title can be implemented, of course, can also be implemented recursively



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.