Modify the page title dynamically in Asp.net 2.0

Source: Internet
Author: User


If you see a good solution on a foreigner's website, you can easily compile it:
In an Asp.net application, you often need to modify the page title dynamically. A typical example is to select a connection in a page navigation control, the relevant content is displayed in the page title. For example, a website has the following website architecture:
There is a category of books, and there are more Chinese books and foreign books below. Generally, you can use a tree or the newly added navigation bar control of Asp.net 2.0.
(Sitemap), such

Books ---> Chinese books;
Books ----> foreign books
At this time, the <title> part of the page can also be displayed, for example, "books --> Chinese books", which is more intuitive and obvious,
In Asp.net 2.0, we can use the <HeadRunat = "server">

Then, you can modify the content of its title in the following form in the page_load event, as shown in
Page. header. Title = "the current time is:" & datetime. Now. tostring ()
You can also write it as page. Title.

Then, we can use this method to combine it with the sitemap control. 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

In the Code above, two constants are predefined first, and then the node of sitemap is gradually established, at the beginning, the node is null, and then call the
getpagetitlebasedonsitenavigation () process. Each time a sitemap is established, the node is connected by a string and finally returned to page. title can be implemented, and 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.