使用c# Code (ASP.net)选择CSS样式
本文关键字:选择 CSS 样式 net ASP Code 使用 | 更新日期: 2023-09-27 17:50:51
我有两个类在我的默认嵌入CSS。aspx页面。如果需要,我可以将代码转移到外部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;
}
所以在点击一个按钮后,我的表类将选择"tftable",另一个按钮将选择"CSS"表类。
对
这不是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
希望你想为这两个样式使用相同的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");
}