Asp. NET page Title single point solution

Source: Internet
Author: User
Tags resource
Asp.net| Resolution | The page recently used the page navigation component SiteMapPath in the. net2.0, and thought of a simple and convenient mechanism to solve the problem of not synchronizing the page title.

The current problems:

First, the entire site ASPX page title usually does not have a unified location, for each page or directly hard-coded page title in the page of the ASPX file, or through Aspx.cs dynamic write in (1.1 also add additional title control, 2.0 can use the Page.title property , and the entire site if a lot of words, this repetitive logic code will be written many times, extremely unattractive, and can not be used in other places to achieve the title information synchronization.

Even if all the page headers are placed in a resource (or XML) file to support multilanguage support, there is also a problem with using the program code to load headers, that is, where the page title is used in the page, it may not only be in the title area (page navigation control), but also how to get those controls from these resource files ( or XML) to get these header information is also a headache problem.

The purpose to be achieved:

First, in the main template throughout the site to solve all the problems of the title of the page, all the titles used in all sites are placed in the Web.sitemap (multilingual) to maintain, so you can simultaneously maintain the navigation information of the page.

Second, in the pop-up form of the title automatically for each site after the title added site name (User login | seven-Si software), but in the page navigation control to refer to the title when the site name does not appear, such as (Home > member Center > User login).

When referencing the Page.title property in a aspx.cs program, you cannot have the site name as shown in the title bar of the page (user login | seven-Si software), but the Page.title value referenced in the normal program of the page is (user login).

The specific implementation methods are as follows:

Add the following code to the masterpage of the Web site:

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:

One, the reason is not (!this. IsPostBack) {} block is implemented because page. The view state cannot be saved in title, which I think is a small bug in the. net2.0.

Second, the reason for overriding the title property in the Render method is that the Render method does not save ViewState while the page is in its lifetime, but from the write page. The title content is valid only in MasterPage because MasterPage is responsible for the render of the title of the page, in the Render method of the next page, title has been render completed, some time change title does not affect the result of the render.

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.