编程解决方案来改变导航标识要突出当前页面ASP.NET [英] Programmatic solution to change navigation id to highlight current page ASP.NET

查看:138
本文介绍了编程解决方案来改变导航标识要突出当前页面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所以它看起来像一个酒吧。为了突出栏上的页面,列表项需要看起来像这样<李ID =当前> 。我不想使用< ASP:&的ContentPlaceHolder GT; 如果我能避免它。有一些code,我可以添加到我的每一个网页来做到这一点还是我坚持使用< 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?

推荐答案

属性添加到您的称为页节母版页

Add a property to your master page called Page Section

public string PageSection { get; set; }

添加页面的MasterType指令的内容页面的顶部

Add a MasterType page directive to the top of your content pages

<%@ MasterType VirtualPath="~/foo.master" %>

在您的内容页code的背后,设置主页的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">

在母版页code后面,使用属性来设置类对身体标记

In the master page code behind, use that property to set a class on the body tag

bodyTag.Attributes.Add("class", this.PageSection);

给每个导航项目的唯一ID属性。

Give each of your nav items a unique ID attribute.

在你的CSS,改变导航项目的基于当前页面类显示

In your css, change the display of the nav items based on the current page class

.home #homeNavItem,
.contact #contactNavItem
{ 
    color: #f00; 
}

这篇关于编程解决方案来改变导航标识要突出当前页面ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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