更改导航 id 以突出显示当前页面 ASP.NET 的编程解决方案
我正在使用 Visual Studio 2008 和 ASP.NET 3.5 编写一个网站。 我设置了一个母版页来简化布局并保留内容页面而不是内容和布局。
导航是列表式的,CSS 的所以它看起来像一个栏。 为了突出显示栏上的页面,列表项需要类似于
。 如果可以避免的话,我不想使用
。 是否有一些代码可以添加到我的每个页面(或只是添加到母版页?)来完成此操作,或者我是否一直使用
的代码?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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您是否考虑过使用 Web.sitemap 文件? 它使它变得非常容易。 如果您的站点地图文件如下所示...
...那么您可以在母版页中执行此操作以获得您想要的结果:
当前页面的 LI 将有一类“activenav”(或您决定使用的任何内容),其他的将有一类“inactivenav”。 你可以相应地编写你的CSS。 这是我在我的网站上使用的技术,效果很好。
Have you considered using a Web.sitemap file? It makes it real easy. If your sitemap file looks like this...
...then you can do this in your master page to achieve the results you want:
The current page's LI will have a class of "activenav" (or whatever you decide to use), and the others will have a class of "inactivenav". You can write your CSS accordingly. This is the technique I use on my site and it works great.
向母版页添加一个名为“页面部分”的属性
将 MasterType 页面指令添加到内容页的顶部
在内容页代码后面,设置母版页的 PageSection 属性
服务器标记
在母版页中,将正文标记设置为 母版页代码后面,使用该属性在正文标记上设置一个类,
为每个导航项提供唯一的 ID 属性。
在您的 CSS 中,根据当前页面类更改导航项的显示
Add a property to your master page called Page Section
Add a MasterType page directive to the top of your content pages
In your content page code behind, set the PageSection property of the master page
In your master page, make the body tag a server tag
In the master page code behind, use that property to set a class on the body tag
Give each of your nav items a unique ID attribute.
In your css, change the display of the nav items based on the current page class
这是一个更好的语义匹配,并且可能是一个更容易设置的变量,以保持导航在各处使用相同的类或 id,并且仅更改 body 元素的 id 来匹配:
然后在每个页面上...
以及在 css 中:
It's a better semantic match and likely an easier variable to set to keep the navigation using the same classes or ids everywhere and only alter the body element's id to match:
and then on each page...
And in the css:
使用或不使用 ContentPlaceHolder 不会影响哪个元素设置了 id="current"。
在渲染菜单组件时,您必须在母版页的代码隐藏中、JavaScript 函数或其他内容中查看某种方法,以便在创建列表时正确地将 id="current" 添加到列表中。
The use or non use of ContentPlaceHolder will not affect which element has the id="current" set on it.
You will have to look at some method, either in your codebehind for the master page, a javascript function or something else when rendering the menu component to properly add the id="current" to the list when it is created.
在母版页代码类中创建受保护的字符串属性怎么样? 覆盖 OnLoad:
然后在您的母版页中 aspx:
那么您就不必弄乱您的 CSS,如果 CSS 来自设计机构,则特别有用。
How about creating a protected string property in your masterpage code class? Override the OnLoad:
Then in your masterpage aspx:
Then you don't have to mess with your CSS, especially useful if the CSS came from a design agency.
以下是我们如何使用 JQuery 通过附加 css 类来更改背景来实现它。
ul.nav
(在 Jquery 中)中的“.nav
”是应用于 UL 标记的 css 类。:contains
帮助检查 ul->li->a 中所有“a”标签/元素的内容,其父节点标题打印在菜单中...如果找到,则应用 css 类命名为 navselected 到特定的 ul->li->a 标签/元素。
此致, Minesh Shah
Systems Plus Pvt。 有限公司
www.systems-plus.com
Here's how we've achieved it using JQuery by appending the css class to change the background.
The "
.nav
" inul.nav
(in Jquery) is the css class applied to the UL tag.:contains
helps checking the contents of all "a" tag/element within the ul->li->a with ParentNode Title that is printed in Menu...If found, applies the css class named navselected to the specific ul->li->a tag/element.
Regards, Minesh Shah
Systems Plus Pvt. Ltd.
www.systems-plus.com
我会使用 javascript 来完成这个任务。 在 css 中,将 #current 更改为类 (.current),然后在您创建的每个 ListItem 上添加 id。 然后使用 RegisterStartupScript 调用一个 javascript 方法来获取适当的 ListItem 并为其分配 current 样式。 使用 Prototype,这看起来像 $('MyPageLi').className = 'current'。
I would use javascript to accomplish this. In css, change your #current to be a class (.current) and then have id's on each of your ListItems that you create. Then using RegisterStartupScript, call a javascript method that gets the appropriate ListItem and assigns it a style of current. Using Prototype, this would look like $('MyPageLi').className = 'current'.