突出显示 ASP.NET Web 应用程序中的菜单栏
我使用 ASP.NET Visual Studio 2010 和母版页构建了一个 Web 应用程序。正如您将看到的,该项目为我们提供了一个默认的菜单栏项。我在这些菜单栏上列出了 5 个页面(链接)。现在,当用户转到特定页面时,我想突出显示该菜单栏链接。我不知道该怎么做:(
我在母版页代码后面尝试了这个,但它也不起作用:
foreach (MenuItem item in NavigationMenu.Items)
{
var navigateUrlParams = item.NavigateUrl.Split('/');
if (Request.Url.AbsoluteUri.IndexOf(navigateUrlParams[navigateUrlParams.Length - 1]) != -1)
{
item.Selected = true;
}
}
在我的标记视图中我有这个:
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" OnMenuItemClick="NavigationMenu_MenuItemClick">
<Items>
<asp:MenuItem Text="Test1"/>
<asp:MenuItem Text="Test2"/>
<asp:MenuItem Text="Test3"/>
</Items>
</asp:Menu>
</div>
所以基本上每当用户来到 Test1.aspx 页面时,我想要菜单项我应该如何做,
谢谢您的帮助!
I built a web application using ASP.NET Visual Studio 2010 with Master Pages. As you will see that the project gives us a default menu bar item. I have 5 pages (links) listed on those menu bars. Now when a user goes to a specific page I want to highlight that menu bar link. I dont know how to do that :(
I tried this on the Master Page Code Behind but it didnt work too:
foreach (MenuItem item in NavigationMenu.Items)
{
var navigateUrlParams = item.NavigateUrl.Split('/');
if (Request.Url.AbsoluteUri.IndexOf(navigateUrlParams[navigateUrlParams.Length - 1]) != -1)
{
item.Selected = true;
}
}
And in my mark up view I have this:
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" OnMenuItemClick="NavigationMenu_MenuItemClick">
<Items>
<asp:MenuItem Text="Test1"/>
<asp:MenuItem Text="Test2"/>
<asp:MenuItem Text="Test3"/>
</Items>
</asp:Menu>
</div>
So basically whenever user comes to Test1.aspx page, I want the menu item of Test1 to be highlighted. How should I do that?
Any help will be appreciated! Thank you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用CSS链接类来实现这一点:
http://www.w3schools.com/css/sel_active.asp
除非您“需要”对这些链接项执行一些编程操作,否则请改用 HTML 锚链接。将它们创建为列表项:
我所说的是询问此处是否需要控件(不熟悉您的项目),并尽可能保持标记简单。
Use CSS link classes to implement this:
http://www.w3schools.com/css/sel_active.asp
And unless you "need" to do something programmatic against these link items, use HTML anchor links instead. Create them as list items:
What I'm saying is to question if controls are necessary here (not familiar with your project), and to keep the markup simple whenever possible.
首先确保您的菜单项都具有 Id 属性
在页面加载处理程序后面的母版页代码中执行类似以下
操作 在本例中,您将检查当前查看的页面的“类型”并将其附加到类属性中当前导航的链接。你可以为 active 定义一个 css 属性,例如
First make sure that your menu items all have an Id property
In your master page code behind in the page Load handler do something like this
In this example you would check the "type" of the currently viewed page and append the to the class attribute for the currently navigated link. you could define a css property for active something like