aspx页面中标题单点解决方案

来源:互联网 发布:鹦鹉螺雾化芯diy数据 编辑:程序博客网 时间:2024/06/10 11:57
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
web页面标题的单点处理技巧

最近在使用.net2.0中ms提供的页面导航组件sitemappath时,想到了一种解决页面标题不同步的一种简单而又方便的机制愿与大家共享。

目前存在的问题:
一、整个网站中aspx页面标题通常没有统一的存放位置,针对每个页面要么把页面标题直接硬编码在页面的aspx文件中,要么通过aspx.cs动态写进去(1.1中还要加入额外的标题控件,2.0中可以使用Page.Title属性),而整个站点如果网页很多的话,这种重复逻辑的代码就要写很多次,极其不美观,并且无法在其它用到标题信息的地方实现同步。
二、即使为了支持多语言支持把所有的页面标题都放入资源(或xml)文件中,通过通一的程序代码来加载标题也会存在一个问题,就是页面中用到页面标题的地方可能不仅在标题区如(页面导航控件),如何让这些控件也从这些资源文件(或xml)中取得这些标题信息也是一个头痛的问题。

要达到的目的:
一、在整个网站中的主模板中一次性解决网页标题的所有问题,所有网站中使用的标题均放在Web.sitemap(可支持多语言)中维护,这样也可以同时同步维护页面的导航信息。
二、在弹出窗体的标题中自动为每一个网站标题后面加入站点名称如(用户登录|七思软件),但在页面导航控件中引用标题时则不出现这个站点名称,如(主页>会员中心>用户登录)。
三、当aspx.cs的程序中引用page.Title属性时不能有站点名称如页面标题栏中显示的为(用户登录|七思软件),但在页面的正常程序中引用的page.Title值为(用户登录)。

具体的实现方法如下:
在网站的masterpage中加入以下代码:


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 = "尚未在Web.sitemap配置此页面导航";
}
}
protected override void Render(HtmlTextWriter writer)
{
this.Page.Title = this.Page.Title " | " Keyss.WebFramework.ConfigManager.GetInstance().SiteName;
base.Render(writer);
}

解释:
一、之所以不在(!this.IsPostBack){}块中实现页面判断是因为page.Title中无法保存视图状态,我觉得这是.net2.0中的一个小bug吧。
二、之所以在render方法重写title属性是因为在页面的生存期中,render方法不会保存viewstate,而从写page.Title内容仅在masterpage中有效是因为masterpage负责页面标题的render,在其后的page的render方法中,title已经render完毕,些时改变title并不会影响render的结果。


出处:七思软件 BLOG

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击