MasterPage 是否知道正在显示哪个页面?
当我使用 MasterPages 在网站上导航时,应用程序是否知道我所在的页面? 如果是这样,它是否将其存储在我可以访问的对象中?
我问的原因是这样我可以替换它:
//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 设置为当前。
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>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我同意克里斯的观点:使用一个控件来处理此菜单的显示,并使其知道应突出显示哪些链接。 这是我经常使用的方法。 如果您有多个页面需要不同样式的相同链接,那么情况可能会变得更加复杂,但您已经明白了。
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.
要从母版页中获取当前请求 URL,您可以执行以下操作:
我还建议将导航移至母版页而不是内容页。 这将使维护/访问变得更容易。
To get the current request URL from within the master page you would do:
I also recommend moving your navigation into the master page instead of the content page. This will make it easier to maintain / access.
是的,在您的主文件中使用以下代码。 它将为您提供内容页面名称。
Yes, Use the below code in your master file. It will give you the content page name.
或者,如果您为子页面设置了特定标题而不是母版页,则可以搜索页面标题尝试:
希望有帮助。
Alternatively you can search for page title if you have set an specific title to the child page instead of masterpage try:
Hope it helps.
这是在 C# 中
this is in C#
您应该能够通过访问 Page 属性来获取该页面。 IE:
You should be able to get the page by accessing the Page property. IE:
您可能只需使用 请求路径 之一设置当前的母版页。 我可能还会在母版页上有一个属性来覆盖它,以便没有链接或其他内容的页面可以将其设置为合理的值。
You'd probably just use one of the Request path from within the master page to set the current. I'd probably also have a property on the master page to override it, so that pages without links or something could set it to something reasonable.
它以这种方式对我有用 - 谢谢 Jared
这就是我所做的,让我们的导航菜单突出显示我们正在查看的页面的当前菜单项。 代码位于母版页中。
你基本上得到了文件路径(贾里德的方式)
我们在链接中使用“~”,所以我不得不将其删除。
迭代 Menu 控件的 menuItems 集合。
比较 navigatorUrl 属性。
(我不是最好的编码员,更不擅长解释 - 但它确实有效,而且我对此非常满意!)
It worked for me this way - Thanks Jared
This is what I did to get our nav menu to highlight the current menu item for the page that we are viewing. The code is in the master page.
You basically get the filepath (Jared's way)
We use the "~" in our links so I had to strip that out.
Iterate the menuItems collection of the Menu control.
Compare the navigateUrl property.
(I'm not the best coder and even worse at explaining - but it works and I was quite chuffed with it!)
导航控件(而不是母版页)应该负责当前突出显示的页面。
加载的页面应该通知导航项是谁,或者导航控件本身应该跟踪它。
关键是母版页应该只是显示内容的容器。它们不应该控制任何东西。
The navigation control, not the master page, should be in charge of what page is currently highlighted.
Either the page that is loaded should notify the navigation item who it is, or the nav control itself should keep track of it.
The point is that master pages are supposed to simply be a holder that content is displayed in. They aren't supposed to control anything.
尝试
它会给你当前页面的母版页。
try
It will get you the master page of the current page.
还有Request.RawURL
There's also the Request.RawURL