C# .NET 网站 - 将 CSS 类添加到菜单元素

发布于 2024-11-14 17:55:31 字数 1253 浏览 1 评论 0原文

我使用 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 &amp; Price">Size &amp; 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 &amp; Price">Size &amp; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2024-11-21 17:55:31

有很多方法可以做到这一点,一种方法是将脚本添加到“从属”页面,以设置适当标签的类。

<li><a href="default.aspx" title="Home" id="aHome" >Home</a></li>
<li><a href="products.aspx" title="Products" id="aProducts">Products</a></li>

然后在 products.aspx 中你可以执行以下操作:

<SCRIPT>document.getElementById('aProducts').setAttribute('class', 'active');</SCRIPT>

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.

<li><a href="default.aspx" title="Home" id="aHome" >Home</a></li>
<li><a href="products.aspx" title="Products" id="aProducts">Products</a></li>

Then in products.aspx you can do:

<SCRIPT>document.getElementById('aProducts').setAttribute('class', 'active');</SCRIPT>
故事未完 2024-11-21 17:55:31

这不是你问题的答案,但这是在CSS类别中,所以我想我至少应该发表评论:)

我强烈建议你在每个列表项上设置静态类,并与CSS中的主体类相结合,使用类似于:

<style type="text/css"><!--
    body.products #menu li.products a,
    body.otherpage1 #menu li.otherpage1 a,
    body.otherpage2 #menu li.otherpage2 a,
    body.otherpage3 #menu li.otherpage3 a,
    body.otherpage4 #menu li.otherpage4 a, { /* your styles here*/ }
--></style>

我认为使用 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:

<style type="text/css"><!--
    body.products #menu li.products a,
    body.otherpage1 #menu li.otherpage1 a,
    body.otherpage2 #menu li.otherpage2 a,
    body.otherpage3 #menu li.otherpage3 a,
    body.otherpage4 #menu li.otherpage4 a, { /* your styles here*/ }
--></style>

I think that using C# in order to just set a class is a little overkill, that's all :)

墨洒年华 2024-11-21 17:55:31

我首先将链接转换为服务器控件。请参阅 MSDN 页面了解。从那里,我将为我的 MasterPage 创建一个接口,这样,如果我将来需要更改 MasterPage,我可以实现相同的接口,而不必担心破坏我的构建(我们将公开公开一些属性)下一步,所以这会有用)。


public interface IMasterPage
{
    String LnkHomeClass { get; set; }
    String LnkProductsClass { get; set; }
    ...
} 

现在,正如我提到的,在母版页的代码隐藏中公开超链接的 CssClass 属性。


public string LnkProductsClass
{
    get 
    {
        return LnkProducts.CssClass;    
    }
    set
    {
        LnkProducts.CssClass = value;
    }
}

现在您可以在子页面上设置这些。


IMasterPage masterPage = Master as IMasterPage;
if (masterPage != null)
{
    masterPage.LnkProductsClass = “active”;
} 

另一种选择,但我不太熟悉,可能是使用 嵌套 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).


public interface IMasterPage
{
    String LnkHomeClass { get; set; }
    String LnkProductsClass { get; set; }
    ...
} 

Now as I mentioned, expose the CssClass attribute for your HyperLinks in the code-behind of your MasterPage.


public string LnkProductsClass
{
    get 
    {
        return LnkProducts.CssClass;    
    }
    set
    {
        LnkProducts.CssClass = value;
    }
}

Now you can set these on your child pages.


IMasterPage masterPage = Master as IMasterPage;
if (masterPage != null)
{
    masterPage.LnkProductsClass = “active”;
} 

Another option, but one that I am less familiar with, may be to use nested MasterPages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文