编程解决方案改变导航id以突出显示当前页ASP.NET [英] Programmatic solution to change navigation id to highlight current page ASP.NET
问题描述
我在使用Visual Studio 2008和ASP.NET 3.5编写网站。我有一个母版页设置简化布局和保持内容页面的内容,而不是内容和布局。
I am writing a website with Visual Studio 2008 and ASP.NET 3.5. I have a masterpage set up to simplify the layout and to keep the content pages for content rather than content and layout.
导航是列表,css'd所以它看起来像一个酒吧。为了突出显示栏上的页面,列表项需要看起来像这样< li id =current>
。我不想使用< asp:ContentPlaceHolder>
如果我可以避免它。有没有一些代码,我可以添加到我的每个页面(或只是到母版页?)来完成这一点,或者我陷入使用< asp:ContentPlaceHolder>
The navigation is list, css'd so it looks like a bar. In order to highlight the page on the bar, the list item needs to look like this <li id="current">
. I do not want to use <asp:ContentPlaceHolder>
if I can avoid it. Is there some code I can add to each of my pages (or just to the masterpage?) to accomplish this or am I stuck using <asp:ContentPlaceHolder>
's?
推荐答案
添加属性到您的母版页
public string PageSection { get; set; }
将MasterType页面指令添加到内容页面的顶部
Add a MasterType page directive to the top of your content pages
<%@ MasterType VirtualPath="~/foo.master" %>
在您的内容页代码中,设置母版页的PageSection属性
In your content page code behind, set the PageSection property of the master page
Master.PageSection = "home";
在您的母版页中,将正文标记设为服务器标记
In your master page, make the body tag a server tag
<body ID="bodyTag" runat="server">
在主页代码后面,使用该属性在body标签上设置类
In the master page code behind, use that property to set a class on the body tag
bodyTag.Attributes.Add("class", this.PageSection);
为每个nav项目分配一个唯一的ID属性。
Give each of your nav items a unique ID attribute.
在css中,根据当前页面类更改nav项目的显示
In your css, change the display of the nav items based on the current page class
.home #homeNavItem,
.contact #contactNavItem
{
color: #f00;
}
这篇关于编程解决方案改变导航id以突出显示当前页ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!