MasterPage 是否知道正在显示的页面? [英] Does a MasterPage know what page is being displayed?

查看:21
本文介绍了MasterPage 是否知道正在显示的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 MasterPages 在网站上导航时,应用程序是否知道我在哪个页面上?如果是这样,它是否将其存储在我可以访问的对象中?

When I navigate on a website utilizing MasterPages, does the application know what page I am on? If so, does it store it in an object I can access?

我问的原因是我可以替换这个:

The reason I am asking is so I can replace this:

//masterpage 
<div id="nav_main">
   <ul><asp:ContentPlaceHolder ID="navigation" runat="server">                    
   </asp:ContentPlaceHolder></ul>
</div>

//content page(s)
<asp:Content ContentPlaceHolderID="navigation" ID="theNav" runat="server">
   <li><a href="default.aspx">Home</a></li>
   <li id="current"><a href="faq.aspx">FAQ</a></li>
   <li><a href="videos.aspx">Videos</a></li>
   <li><a href="#">Button 4</a></li>
   <li><a href="#">Button 5</a></li>
</asp:Content>

使用更优雅的导航解决方案,通过将列表项的 ID 设置为当前"来突出显示指向页面的链接.目前,每个页面都会重新创建导航,并将其各自链接的 ID 设置为 current.

With a more elegant solution for the navigation, which highlights the link to the page by having the list item's ID set to "current". Currently each page recreates the navigation with its respective link's ID set to current.

推荐答案

我同意 Chris:使用控件来处理此菜单的显示,并使其知道应该突出显示哪个链接.这是我经常使用的一种方法.如果您有多个页面需要不同样式的相同链接,这可能会变得更加复杂,但您明白了.

I'd concur with Chris: use a control to handle display of this menu and make it aware of what link should be highlighted. Here's a method I use regularly. It may become more complex if you've got multiple pages that would need the same link styled differently, but you get the idea.

Dim thisURL As String = Request.Url.Segments(Request.Url.Segments.Count - 1)
Select Cast thisUrl
   Case "MenuItem1.aspx"
       lnkMenu1.CssClass = "Current"
   Case "MenuItem2.aspx"
       lnkMenu2.CssClass = "Current"
End Select

这篇关于MasterPage 是否知道正在显示的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆