ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ASP.NET 2.0中动态修改页面标题

ASP.NET 2.0中动态修改页面标题

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-01-04 15:35:20 0 删除 编辑

在老外的站上看到解决的好方法,故简单编译之:

在一个asp.net 的应用中,经常要动态修改页面的标题,一个典型的例子就是,在一个页面导航的控件中,希望用户点选哪一个连接,在页面的title里就显示相关的内容,举个例子,比如一个网站,有如下的网站架构:

有图书分类,下面再有中国图书,外国图书分类,则一般可以用树形或者asp.net 2.0的新增加的导航栏控件(sitemap),来实现,比如

图书--->中国图书;图书---->外国图书等,而如果这个时候,能在页面的部分,也能显示比如"图书-->中国图书"这样,那就更加直观明显了,在asp.net 2.0中,我们可以使用<head>部分的服务端控件来实现了,首先,要添加标记<head runat="server"></P> <P>然后可以在page_load事件中,以如下形式改边其title的内容了,如</P> <P>Page.Header.Title = "The current time is: " & DateTime.Now.ToString() ,也可以简单写成page.title</P> <P>然后,我们可以通过这样的办法,将其于sitemap控件结合了,实现方法如下:</P> <P> <TABLE style="BORDER-BOTTOM: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-RIGHT: #cccccc 1px dotted" border=0 cellSpacing=0 cellPadding=6 width="95%" align=center> <TBODY> <TR> <TD style="WORD-WRAP: break-word" bgColor=#fdfddf><FONT color=#ff0000>以下为引用的内容:</FONT><BR> <P>Const DEFAULT_UNNAMED_PAGE_TITLE As String = "Untitled Page"<BR>    Const DEFAULT_PAGE_TITLE As String = "Welcome to my Website!!"</P> <P>    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<BR>        'Set the page's title, if needed<BR>        If String.IsNullOrEmpty(Page.Title) OrElse Page.Title = DEFAULT_UNNAMED_PAGE_TITLE Then<BR>            If SiteMap.CurrentNode Is Nothing Then<BR>                Page.Title = DEFAULT_PAGE_TITLE<BR>            Else<BR>                Page.Title = GetPageTitleBasedOnSiteNavigation()</P> <P>                'Can also use the following if you'd rather<BR>                'Page.Title = GetPageTitleBasedOnSiteNavigationUsingRecursion(SiteMap.CurrentNode)<BR>            End If<BR>        End If<BR>    End Sub</P> <P>    Private Function GetPageTitleBasedOnSiteNavigation() As String<BR>        If SiteMap.CurrentNode Is Nothing Then<BR>            Throw New ArgumentException("currentNode cannot be Nothing")<BR>        End If</P> <P>        'We are visiting a page defined in the site map - build up the page title<BR>        'based on the site map node's place in the hierarchy</P> <P>        Dim output As String = String.Empty<BR>        Dim currentNode As SiteMapNode = SiteMap.CurrentNode</P> <P>        While currentNode IsNot Nothing<BR>            If output.Length > 0 Then<BR>                utput = currentNode.Title & " :: " & output<BR>            Else<BR>                utput = currentNode.Title<BR>            End If</P> <P>            currentNode = currentNode.ParentNode<BR>        End While</P> <P>        Return output<BR>    End Function</P></TD></TR></TBODY></TABLE></P> <P>在上面的代码中,首先预定义了两个常量,然后逐步建立sitemap的结点,一开始结点是null的,然后再调用GetPageTitleBasedOnSiteNavigation() 这个过程,在每建立一个sitemap的结点时,用字符串进行连接,最后返回给page.title即可实现,当然也可以用递归实现。</P> <p style="clear:both;"></p> <p class="translate"> 来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-526497/,如需转载,请注明出处,否则将追究法律责任。 </p> </div> </div> <div class="preview-footer clearfix"> <!--已点赞、收藏--> <div class="icon-praise" id="praise" onclick="blogObj.praise($(this),'/praise/')"><span class="text"></span><span class="num">0</span></div> <div class="icon-collect" id="collect" onclick="blogObj.praise($(this),'/favour/')"><span class="text"></span><span class="num">0</span></div> <div class="share-group fr"> <div class="bdsharebuttonbox bdshare-button-style0-16" data-bd-bind="1522216707615"> <span class="fl">分享到:</span> <a href="javascript:;" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a> <a href="javascript:;" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a> <a href="javascript:;" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a> <a href="javascript:;" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a> </div> <script> window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdDesc": "", "bdMini": "", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "32" }, "share": {"bdSize": 16} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)]; if ($(window).width() < 1320) { if ($(window).width() < 1200) { $(".sideslip").css({ "left": "20px", "margin-left": 0 }) } else { $(".sideslip").css({ "margin-left": "-590px" }) } }</script> </div> </div> <div class="article-context"> <div class="fl"> <span class="color77">上一篇:</span> <a href="http://blog.itpub.net/12639172/viewspace-526496/">ADO.NET三个核心对象的简要说明</a> </div> <div class="fr"> <span class="color77">下一篇:</span> <a href="http://blog.itpub.net/12639172/viewspace-526498/">雅蛙网ajax特效jQuery实现方法</a> </div> </div> <input type="hidden" id="blogId" value="526497"> <!--评论开始--> <div class="blog-comment"> <div class="new-comment"> <input type="hidden" id="hid" name="hid" value="526497"> <input type="hidden" name="_token" value="uiFiYNAb5nMumHDBGHqHXp9CAcp3e4BcZZNvwqHD"> <a class="avatar" href="javascript:;"> <img src="http://blog.itpub.net/images/user_pic_default.png"></a> <!--用户未登录--> <div class="sign-container"> <span>请登录后发表评论</span> <a class="btn btn-sign"> <a class="layui-btn ml30" onclick="blog.login(event)">登录</a> </a> </div> <!--用户已登录--> <textarea placeholder="请写下你的评论…" onfocus="blogObj.textareaFocus($(this),200)" maxlength=200></textarea> </div> <div class="comment-list"> <div class="top-title"><span class="all-comment">全部评论</span> <span class="colorbb ml20"></span> </div> <div class="comment-items" id="comment_items"> <script type="text/template" id='blog_template'> <%for(var i=0;i <items.length;i++){%> <div class="comment-item"> <div class="author"> <a target="_blank" href="/<%=items[i].uid%>" class="avatar"> <img src="<%=items[i].headimg%>" alt=""> </a> <div class="info"> <div class="name"><a target="_blank" href="/<%=items[i].uid%>"><%=items[i].username%></a> <span class="fr reply-btn" onclick="blogObj.newComment($(this),'<%=items[i].username%>',<%=items[i].id%>)">回复</span></div> <div class="time colorbb"><%=items[i].createtime%></div> </div> </div> <p class="comment-wrap"><%=items[i].content%></p> <%if(items[i].items.items.length) { %> <div class="sub-comment-list"> <%for(var j=0;j<items[i].items.items.length;j++){%> <div class="sub-comment"> <div class="color77 time-reply"> <span class="time"><%=items[i].items.items[j].createtime%></span> <span class="reply-btn fr" onclick="blogObj.newComment($(this),'<%=items[i].items.items[j].username%>',<%=items[i].items.items[j].id%>)">回复</span> </div> <p><a href="/<%=items[i].uid%>"><%=items[i].items.items[j].username%></a>   回复   <a href="/<%=items[i].touid%>"><%=items[i].items.items[j].tousername%></a>: <span class="content"><%=items[i].items.items[j].content%></span></p> </div> <%}%> <%if(items[i].items.total > 5) { %> <div class="more-comment"><span class="sub-comment-count">还有<i class="count"><%=items[i].items.total-5%></i>条评论</span><span class="more-comment-btn" onclick=blogObj.loadSubComment($(this),<%=items[i].id%>) data-count=1 data-flag=true>点击查看</span></div> <%}%> </div> <%}%> </div> <%}%> </script> </div> </div> </div> <!--评论结束--> </div> <script> </script> <div class="fr w290"> <!--作者信息开始--> <!--作者信息开始--> <div class="author-info right-fixed "> <div class="head-img"> <a href="http://blog.itpub.net/12639172/"><img src="http://account.itpub.net/api/avatar.php?uid=12639172" alt=""></a> </div> <div class="author-name"><a href="http://blog.itpub.net/12639172/">iDotNetSpace</a></div> <div class="author-intro"> </div> <p class="register-time"><span class="color77">注册时间:</span>2008-01-04</p> <ul class="tree-list clearfix"> <li> <div class="item-tt">博文量</div> <a href="http://blog.itpub.net/12639172/"><span class="item-num blognum">2376</span></a> </li> <li> <div class="item-tt">访问量</div> <div class="item-num blogviewnum">5321146</div> </li> </ul> </div> <!--作者信息结束--> <script> var data = {uid: 12639172}; $._ajax({ url: '/getAuthorInfo/', data: data, type: 'get', dataType: 'json', success: function (data) { if (data.code == 200) { var data = data.data; $(".blognum").text(data.blognum); $(".blogviewnum").text(data.visitednum); } } }); </script> <!--作者信息结束--> <!--博文推荐开始--> <div class="blog-choice right-fixed"> <h3 class="choice-title">最新文章</h3> <ul class="newul"> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683059/" class="clearfix"> ASP.NET入门五步详解 </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683057/" class="clearfix"> ASP.NET配置错误页面浅析 </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683056/" class="clearfix"> 介绍ASP.NET控件ID </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683055/" class="clearfix"> ASP.NET中批量删除--只调用一次存储过程 </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683054/" class="clearfix"> C#后台调用前台javascript的五种方法 </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683051/" class="clearfix"> 学习ASP.NET的流程 </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683050/" class="clearfix"> 详解ASP.NET页面的aspx扩展 </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683049/" class="clearfix"> asp.net中GridView导出数据 </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-683048/" class="clearfix"> ASP.NET中获取URL重写前的原始地址 </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/12639172/viewspace-682721/" class="clearfix"> C#常用线程同步方法应用场景和实现原理 </a> </li> <ul> </div> <!--大牛精选结束--> </div> </div> <div class="icon-back-top" onclick="$('html,body').animate({scrollTop:0},'slow');"></div> <!--main部分结束--> <!--百度推送--> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <!--2019-08-17 22:45:59--> <!--footer部分开始--> <div class="blog-footer"> <div class="w1200 pr"> <div class="footer-links"> <a class="icon-blog icon-wx mr30"> <div class="wx-qrcode"> <img src="http://edu.itpub.net/images/qrcode.jpg" alt=""> </div> </a> <a href="https://weibo.com/itpub2001?from=myfollow_all" class="icon-blog icon-wb" target="_blank"></a> </div> <p class="footer-nav"> <a href="http://www.it168.com/bottomfile/it168.shtml" target="_blank">支持我们</a> <a href="http://www.it168.com/bottomfile/tgzn.shtml" target="_blank">作者招募</a> <a href="http://www.it168.com/bottomfile/sytk.shtml" target="_blank">用户协议</a> <a href="http://blog.itpub.net/31509949/viewspace-2157750/" target="_blank">FAQ</a> <a href="http://edu.itpub.net/contactus.html" target="_blank">Contact Us</a> <script src="https://s22.cnzz.com/z_stat.php?id=1274521965&web_id=1274521965" language="JavaScript"></script> </p> <p>北京盛拓优讯信息技术有限公司. 版权所有  <a style="color:#777777;" target="_blank" href="http://beian.miit.gov.cn">京ICP备09055130号-4</a>  北京市公安局海淀分局网监中心备案编号:11010802021510</p> <p>广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员</p> </div> </div> <!--footer部分结束--> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?5016281862f595e78ffa42f085ea0f49"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <!-- END STAT PV --> </body> </html>