問題描述
當我使用 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 是否知道正在顯示的頁面?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!