C#ASP.NET如何通过后台代码修改CSS类 [英] C# ASP.NET How to modify CSS class via code-behind

查看:129
本文介绍了C#ASP.NET如何通过后台代码修改CSS类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,就像标题一样,如何通过代码隐藏来修改CSS类呢? 例如以下代码

So just like the title, how to modify the CSS class via code-behind? For example the following code

                    <li class="active"><a href="pages-messages.html"><span class="fa fa-comments"></span> Messages</a></li>
                    <li><a href="pages-calendar.html"><span class="fa fa-calendar"></span> Calendar</a></li>
                    <li><a href="pages-tasks.html"><span class="fa fa-edit"></span> Tasks</a></li>
                    <li><a href="pages-content-table.html"><span class="fa fa-columns"></span> Content Table</a></li>
                    <li><a href="pages-faq.html"><span class="fa fa-question-circle"></span> FAQ</a></li>
                    <li><a href="pages-search.html"><span class="fa fa-search"></span> Search</a></li>

上面的代码是我的HTML页面的导航,第一行具有li class ="active",如何将其更改为li,而第二行(pages-calender)将其更改为li class ="active"?

The code above is navigation for my HTML page and the first line has li class="active", how to change it into just li and the second row which is pages-calender change it into li class="active" ?

谢谢

推荐答案

如果使用的是WebForms,则添加runat="server"属性,并为每个需要在后台代码中修改的li标记设置唯一的id属性. 需要添加这些标签,以实现从代码隐藏到HTML元素的编程访问.

If you are using WebForms, then add runat="server" attributes and set unique id attribute for each li tag you need to modify in code-behind. Addition of this tags is needed to enable programmatic access from code-behind to the HTML element.

例如:

<li runat="server" id="PagesMessages" class="active"><a href="pages-messages.html"><span class="fa fa-comments"></span> Messages</a></li>
<li runat="server" id="PagesCalender"><a href="pages-calendar.html"><span class="fa fa-calendar"></span> Calendar</a></li>

然后在代码背后:

PagesMessages.Attributes["class"] = string.Empty;
PagesCalender.Attributes["class"] = "active";

这篇关于C#ASP.NET如何通过后台代码修改CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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