【问题标题】:Choose CSS style using C# Code (ASP.net)使用 C# 代码 (ASP.net) 选择 CSS 样式
【发布时间】:2014-04-25 08:42:16
【问题描述】:

我的 Default.aspx 页面的嵌入式 CSS 中有两个类。如果需要,我可以将代码转移到外部 CSS。

我可以在 C# 中编写一个代码,在单击按钮后,我可以在两种样式之间为我的表格进行选择吗? 我对 ASP 和 C# 非常陌生。

我的课程(嵌入式):

   .tftable
    {
        background-color: Blue;
        font-size: 12px;
        color: #333333;
        margin: 0px;
        padding: 0px;
        width: 100%;
        border-width: 1px;
        border-color: #729ea5;
        border-collapse: collapse;
    }

    .CSSTable
    {
        background-color: Gray;
        margin: 0px;
        padding: 0px;
        width: 100%; /*Fits the <div>*/
        box-shadow: 10px 10px 5px #888888;

    }

所以点击 1 个按钮后,我的表格类将选择“tftable”,另一个按钮将选择“CSS”表格类。

问候

【问题讨论】:

  • 您想在服务器端或客户端更改这个简单的 CSS 类吗?您需要在服务器中执行任何逻辑 onclick 按钮还是只想更改此 css 类?
  • 服务器端,通过C#代码点击按钮。

标签: c# html asp.net css


【解决方案1】:

不是c#代码,是javascript代码

添加ID如下的样式链接

<link href="css/style1.css" type="text/css" rel="stylesheet" id="stylesheet" />

然后创建一个Javascript函数来更改Css文件

function changeStyle(name){
    if(name=='style1')
        document.getElementById('stylesheet').href='css/style1.css';
    else if(name=='style2')
        document.getElementById('stylesheet').href='css/style2.css';
    else if(name=='style3')
        document.getElementById('stylesheet').href='css/style3.css';
}

在您的按钮中调用该函数

<ul>
<li><a href="javascript:changeStyle('style1')">Style1</a></li>
<li><a href="javascript:changeStyle('style2')">Style2</a></li>
<li><a href="javascript:changeStyle('style3')">Style3</a></li>
</ul>

参考

http://www.qualitycodes.com/tipdemos/javascript/dynamic-css/ http://www.qualitycodes.com/tip/14/dynamically-choosingchanging-a-css-file.html

【讨论】:

    【解决方案2】:

    希望您对两种样式都使用相同的 css 文件,如果是这样,请尝试一次。

    protected void Button1_Click(object sender, EventArgs e)
    {
        myTable.Attributes.Add("class", "tftable");
    }
    
    protected void Button2_Click(object sender, EventArgs e)
    {
        myTable.Attributes.Add("class", "CSSTable");
    }
    

    【讨论】:

    • 我认为您的意思是使用class 属性
    • 只有myTable.Attributes("style") = xxx 也会更简单。无需删除即可重新添加
    • 如果属性集合中已经有class,则使用.Add 将失败...请仔细查看我的第二条评论。 (我刚刚注意到我在第二条评论中使用了style,显然应该是myTable.Attributes("class") = xxx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2010-09-29
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多