All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
ASP.NET,2.0中动态修改页面标题_[Asp.Net教程]

Writer: delv Article type: Programming skills(编程技巧) Time: 2014/1/6 8:48:02 Browse times: 178 Comment times: 0

ASP.NET,2.0中动态修改页面标题_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

ASP.NET 2.0中动态修改页面标题_[Asp.Net教程]























在老外的站上看到解决的好方法,故简单编译之:
在一个asp.net 的应用中,经常要动态修改页面的标题,一个典型的例子就是,在一个页面导航的控件中,希望用户点选哪一个连接,在页面的title里就显示相关的内容,举个例子,比如一个网站,有如下的网站架构:
有图书分类,下面再有中国图书,外国图书分类,则一般可以用树形或者asp.net 2.0的新增加的导航栏控件
(sitemap),来实现,比如




图书--->中国图书;
图书---->外国图书
等,而如果这个时候,能在页面的部分,也能显示比如"图书-->中国图书"这样,那就更加直观明显了,<BR>在asp.net 2.0中,我们可以使用<head>部分的服务端控件来实现了,首先,要添加标记<BR><head runat="server"></P><br/><br/><br/><P>然后可以在page_load事件中,以如下形式改边其title的内容了,如<BR>Page.Header.Title = "The current time is: " & DateTime.Now.ToString() <BR>,也可以简单写成page.title.</P><br/><br/><br/><P>然后,我们可以通过这样的办法,将其于sitemap控件结合了,实现方法如下:</P><br/><br/><br/><P>Const DEFAULT_UNNAMED_PAGE_TITLE As String = "Untitled Page"<BR> Const DEFAULT_PAGE_TITLE As String = "Welcome to my Website!!"</P><br/><br/><br/><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><br/><br/><br/><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><br/><br/><br/><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><br/><br/><br/><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><br/><br/><br/><P> Dim output As String = String.Empty<BR> Dim currentNode As SiteMapNode = SiteMap.CurrentNode</P><br/><br/><br/><P> While currentNode IsNot Nothing<BR> If output.Length > 0 Then<BR> output = currentNode.Title & " :: " & output<BR> Else<BR> output = currentNode.Title<BR> End If</P><br/><br/><br/><P> currentNode = currentNode.ParentNode<BR> End While</P><br/><br/><br/><P> Return output<BR> End Function</P><br/><br/><br/><P>  在上面的代码中,首先预定义了两个常量,然后逐步建立sitemap的结点,一开始结点是null的,然后再调用GetPageTitleBasedOnSiteNavigation() 这个过程,在每建立一个sitemap的结点时,用字符串进行连接,最后返回给page.title即可实现,当然也可以用递归实现.</P><br/><br/><br/><br/><br/>来源:<FONT color=#ff0000>博客园</FONT> <br/><br/><br/><br/><br/> <br/><br/><br/><br/><br/> <br/><br/><br/> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/></div> <textarea name="TextBoxNews" rows="2" cols="20" id="TextBoxNews" style="height:0px;width:0px;"> </textarea><hr style="width:400px; height:1px;" /> <input type="button" value="upload picture(上传图片)" onclick="iFrameDisplay()"/> <iframe id="iframepage" src="./UploadNewsVideoPageToLocalHost.aspx" scrolling="no" frameborder="0" width="100%" style="display:none;"></iframe><br /> <input type="hidden" name="HiddenFieldPicPath" id="HiddenFieldPicPath" /><br /> </td> </tr> </table> </div> <hr style="width:800px; height:1px;" /> <div style="width:800px; height:auto; background-color:#FFFFCC;"> <div id="UpdatePanel2"> <table style="width:800px; height:20px;"> <tr> <td style="width:300px; height:20px; font-family:黑体; font-size:small; color:#555;" align="left"> There are 0 records, </td> <td style="width:500px; height:20px;"> </td> </tr> </table> </div> </div> <div id="UpdatePanel1"> <div id="commentPanel" style="width:800px; height:310px; background-color:#FFFFCC;"> <table style="width:800px; height:300px;"> <tr> <td style="width:200px; height:50px; font-family:黑体;" align="right"> Comment: </td> <td style="width:600px; height:200px;" rowspan="2"> <textarea name="TextBoxUserComment" rows="2" cols="20" id="TextBoxUserComment" style="color:#404040;font-family:新宋体;font-size:Larger;height:200px;width:600px;"> </textarea></td> </tr> <tr> <td style="width:200px; height:150px;">Must be registered users to comment(必须是注册用户才能发表评论)</td> </tr> <tr> <td style="width:200px; height:50px;"></td> <td style="width:600px; height:50px;"> <span id="RequiredFieldValidator1" style="visibility:hidden;">Comments can't is empty(评论不能为空)</span> <input type="submit" name="ButtonUserComment" value="Comment" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ButtonUserComment", "", true, "", "", false, false))" id="ButtonUserComment" style="color:Red;background-color:#FFFFCC;border-color:#FFFFCC;" /> <span id="LabelNewsComment" style="display:inline-block;color:Red;width:400px;"></span></td> </tr> </table> </div> </div> <script type="text/javascript"> //<![CDATA[ var Page_Validators = new Array(document.getElementById("RequiredFieldValidator1")); //]]> </script> <script type="text/javascript"> //<![CDATA[ var RequiredFieldValidator1 = document.all ? document.all["RequiredFieldValidator1"] : document.getElementById("RequiredFieldValidator1"); RequiredFieldValidator1.controltovalidate = "TextBoxUserComment"; RequiredFieldValidator1.errormessage = "RequiredFieldValidator"; RequiredFieldValidator1.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid"; RequiredFieldValidator1.initialvalue = ""; //]]> </script> <script type="text/javascript"> //<![CDATA[ var a=0;if(a==0){alert('Please log in(请您登录!)');location='Userlogin.aspx';} var Page_ValidationActive = false; if (typeof(ValidatorOnLoad) == "function") { ValidatorOnLoad(); } function ValidatorOnSubmit() { if (Page_ValidationActive) { return ValidatorCommonOnSubmit(); } else { return true; } } document.getElementById('RequiredFieldValidator1').dispose = function() { Array.remove(Page_Validators, document.getElementById('RequiredFieldValidator1')); } //]]> </script> </form> </center> <center > <!--网站底部--> <div style=" width:1200px; background-color:#FFFFFF;"> <hr style=" width:1200px; height:1px;"/> <table> <tr style=" height:50px; width:1200px;"> <td align="center" style="height:50px;"> <a href="disclaimer.html" style="font-family:黑体; font-size:small; color:#555;" target="_blank">Disclaimer</a> </td> <td align="center" style="height:50px;"> <a href="privacy policy.html" style="font-family:黑体; font-size:small; color:#555;" target="_blank">Privacy Policy</a> </td> <td align="center" style="height:50px;"> <a href="AboutUs.html" style="font-family:黑体; font-size:small; color:#555;" target="_blank">About us</a> </td> <td align="center" style="height:50px;"> <a href="SiteMap.html" style="font-family:黑体; font-size:small; color:#555;" target="_blank">Site Map</a> </td> <td align="center" style="height:50px;"> <!--百度网站统计--> <script type="text/javascript"> var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Fcd1d276abd98aad311a7e9ab1f772b56' type='text/javascript'%3E%3C/script%3E")); </script> </td> <td align="center" style="height:50px;"> </td> <td align="center" style="height:50px;"> <a href="http://webscan.360.cn"><img border="0" src="http://webscan.360.cn/status/pai/hash/494b6dad04e216e4d1f270ad7bfe37f4" alt=""/></a> </td> </tr> </table> <table> <tr> <td style="width:120px; height:30px;" align="left"> Copyright ©2011-</td> <td style="width:50px; height:30px;" align="left"><div id="yearTime"></div> </td> <td style="width:300px; height:30px;" align="left"> uuhomepage.com, Inc. All rights reserved. </td> </tr> </table> </div> </center> </body> </html>