asp.net 2.0中动态修改页面标题 |
<script src="/adfile/stop.js" type="text/javascript"></script> |
在老外的站上看到解决的好方法,故简单编译之: 在一个asp.net 的应用中,经常要动态修改页面的标题,一个典型的例子就是,在一个页面导航的控件中,希望用户点选哪一个连接,在页面的title里就显示相关的内容,举个例子,比如一个网站,有如下的网站架构: 有图书分类,下面再有中国图书,外国图书分类,则一般可以用树形或者asp.net 2.0的新增加的导航栏控件 (sitemap),来实现,比如 图书--->中国图书; 图书---->外国图书 等,而如果这个时候,能在页面的<title>部分,也能显示比如"图书-->中国图书"这样,那就更加直观明显了, 在asp.net 2.0中,我们可以使用<head>部分的服务端控件来实现了,首先,要添加标记 <head runat="server"> 然后可以在page_load事件中,以如下形式改边其title的内容了,如 Page.Header.Title = "The current time is: " & DateTime.Now.ToString() ,也可以简单写成page.title. 然后,我们可以通过这样的办法,将其于sitemap控件结合了,实现方法如下: Const DEFAULT_UNNAMED_PAGE_TITLE As String = "Untitled Page" Const DEFAULT_PAGE_TITLE As String = "Welcome to my Website!!"
'Can also use the following if you'd rather Private Function GetPageTitleBasedOnSiteNavigation() As String 'We are visiting a page defined in the site map - build up the page title Dim output As String = String.Empty While currentNode IsNot Nothing currentNode = currentNode.ParentNode Return output |