C# .NET 网站 - 将 CSS 类添加到菜单元素
我使用 PHP 已经有 7 年左右的时间了,但从今天起我开始在一个项目中使用 .NET。仅用于网站:
该项目具有:
.NET4 Viewstate = false
现在我的问题是这样的。主要布局被母版页覆盖,看起来很明显它是如何工作的。它还包括导航菜单选项,例如:
<div id="menu">
<ul>
<li><a href="default.aspx" title="Home" >Home</a></li>
<li><a href="products.aspx" title="Products">Products</a></li>
<li><a href="prices.aspx" title="Size & Price">Size & Price</a></li>
<li><a href="formats.aspx" title="File Formats">File Formats</a></li>
</ul>
</div>
现在我想做的是根据我所在的页面添加 CSS 类属性,因此如果我在产品页面上,我会得到以下来源:
<div id="menu">
<ul>
<li><a href="default.aspx" title="Home" >Home</a></li>
<li><a href="products.aspx" class="active" title="Products">Products</a></li>
<li><a href="prices.aspx" title="Size & Price">Size & Price</a></li>
<li><a href="formats.aspx" title="File Formats">File Formats</a></li>
</ul>
</div>
任何帮助将不胜感激!
干杯:)
I've been using PHP for 7 years or so now, but as of today I've been thrust into using .NET for a project. Just for an insite:
This project features:
.NET4
Viewstate = false
Now my issue is this. The main layout is covered by the master page which seems quiet obvious how it works. It also includes nav menu options such as:
<div id="menu">
<ul>
<li><a href="default.aspx" title="Home" >Home</a></li>
<li><a href="products.aspx" title="Products">Products</a></li>
<li><a href="prices.aspx" title="Size & Price">Size & Price</a></li>
<li><a href="formats.aspx" title="File Formats">File Formats</a></li>
</ul>
</div>
Now what I would like to do would be to add a CSS class attribute depending on what page I'm on so if I was on the products page I'd get the following source:
<div id="menu">
<ul>
<li><a href="default.aspx" title="Home" >Home</a></li>
<li><a href="products.aspx" class="active" title="Products">Products</a></li>
<li><a href="prices.aspx" title="Size & Price">Size & Price</a></li>
<li><a href="formats.aspx" title="File Formats">File Formats</a></li>
</ul>
</div>
Any help would be greatly appreciated!
Cheers :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有很多方法可以做到这一点,一种方法是将脚本添加到“从属”页面,以设置适当标签的类。
然后在 products.aspx 中你可以执行以下操作:
Lots of ways to do this, one way is to add a script to the "slave" page that sets the class of the appropriate tag.
Then in products.aspx you can do:
这不是你问题的答案,但这是在CSS类别中,所以我想我至少应该发表评论:)
我强烈建议你在每个列表项上设置静态类,并与CSS中的主体类相结合,使用类似于:
我认为使用 C# 来只是设置一个类有点矫枉过正,仅此而已:)
It's not an answer to your question, but this was in the CSS category so I figured I'd at least comment :)
I would strongly suggest that you set static classes on each list item, and combined with body classes in the CSS, use something like:
I think that using C# in order to just set a class is a little overkill, that's all :)
我首先将链接转换为服务器控件。请参阅 MSDN 页面了解 。从那里,我将为我的 MasterPage 创建一个接口,这样,如果我将来需要更改 MasterPage,我可以实现相同的接口,而不必担心破坏我的构建(我们将公开公开一些属性)下一步,所以这会有用)。
现在,正如我提到的,在母版页的代码隐藏中公开超链接的 CssClass 属性。
现在您可以在子页面上设置这些。
另一种选择,但我不太熟悉,可能是使用 嵌套 MasterPages< /a>.
I'd start by converting the links to server controls. See the MSDN page for <asp:HyperLink />. From there, I'd make an interface for my MasterPage, so that if I ever need to change MasterPages in the future, I can implement the same interface and not have to worry about breaking my build (we'll publicly expose some properties in the next step, so this will be useful).
Now as I mentioned, expose the CssClass attribute for your HyperLinks in the code-behind of your MasterPage.
Now you can set these on your child pages.
Another option, but one that I am less familiar with, may be to use nested MasterPages.