Single-point processing skills for web page titles.
Recently, when using the page navigation component sitemappath provided by ms in .net2.0, I thought of a simple and convenient mechanism to solve the problem of page title desynchronization. I would like to share it with everyone.
Current problems:
1. There is usually no unified storage location for aspx page titles in the entire website. For each page, the page title is either hard-coded directly in the aspx file of the page, or dynamically written in through aspx.cs (additional titles are also added in 1.1 Control, the Page.Title property can be used in 2.0), and if the entire site has a lot of web pages, this repetitive logic code will have to be written many times, which is extremely unsightly and cannot be synchronized in other places where title information is used.
2. Even if all page titles are put into resource (or xml) files in order to support multi-language support, there will be a problem in loading titles through Tongyi program code, that is, the places where page titles are used in the page may not only In the title area such as (page navigation control), how to let these controls also obtain the title information from these resource files (or xml) is also a headache.
What to achieve:
1. Solve all problems with web page titles at once in the main template of the entire website. Titles used in all websites are maintained in Web.sitemap (can support multiple languages), so that the navigation information of the page can also be maintained simultaneously. .
2. Automatically add the site name after each website title in the title of the pop-up form, such as (User Login | Qisi Software), but when referencing the title in the page navigation control, the site name will not appear, such as (Home>Member Center>User Login).
3. When the page.Title attribute is referenced in the aspx.cs program, there cannot be a site name. For example, the page title bar displays (User Login | Qisi Software), but the page.Title value referenced in the normal program of the page is (User Login).
The specific implementation method is as follows:
Add the following code to the masterpage of the website:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.searchDescription.Content = this.SearchDescription;
this.searchKeywords.Content = this.SearchKeywords;
}
if (SiteMap.CurrentNode != null)
{
this.Page.Title = SiteMap.CurrentNode.Title;
}
else
{
this.Page.Title = "This page navigation has not been configured in Web.sitemap";
}
}
protected override void Render(HtmlTextWriter writer)
{
this.Page.Title = this.Page.Title + " | " + Keyss.WebFramework.ConfigManager.GetInstance().SiteName;
base.Render(writer);
}
explain:
1. The reason why page judgment is not implemented in the (!this.IsPostBack){} block is because the view state cannot be saved in page.Title. I think this is a small bug in .net2.0.
2. The reason why the title attribute is rewritten in the render method is because the render method does not save the viewstate during the lifetime of the page, and the page.Title content written from the page is only valid in the masterpage because the masterpage is responsible for the rendering of the page title. In the render method of the page, the title has been rendered. Changing the title will not affect the rendering result.
Source: Qisi Software BLOG