从后面的 asp.net 代码编辑 css 页面
edit css page from asp.net code behind
我想使用 asp.net 后面的代码以编程方式编辑 css 文件,而且我尝试了一些命令但没有结果。
解释一下我的情况:我应该从 file.aspx.cs 修改 file.css(.class1 存在)。例如添加此属性:
.class {backgroud-color:grey}
我做了这个:
<link href="file.css" rel="stylesheet" id="boxcss" runat="server" />
在后面的代码中:
string iframe = "iframe{border-radius:300px}";
boxcss.Attributes.Add("class",iframe);
感谢您的帮助
您可以使用以下代码将样式属性添加到现有 .class1
选择器或创建新的 .class2
选择器:
Style class1 = new Style();
class1.BackColor = Color.Orange;
Header.StyleSheet.CreateStyleRule(class1, null, ".class1");
Style class2 = new Style();
class2.BorderColor = Color.Red;
class2.BorderStyle = BorderStyle.Solid;
class2.BorderWidth = Unit.Pixel(5);
Header.StyleSheet.CreateStyleRule(class2, null, ".class2");
但是,样式 class 不允许访问 border-radius
。您可以在 Literal 控件的帮助下将该样式属性添加到 Header:
Literal iframeStyle = new Literal();
iframeStyle.Text = "<style>iframe { border-radius: 300px; }</style>";
Header.Controls.Add(iframeStyle);
注意:head
标记必须具有 runat="server"
属性才能在代码隐藏中访问:
<head runat="server">
我想使用 asp.net 后面的代码以编程方式编辑 css 文件,而且我尝试了一些命令但没有结果。 解释一下我的情况:我应该从 file.aspx.cs 修改 file.css(.class1 存在)。例如添加此属性:
.class {backgroud-color:grey}
我做了这个:
<link href="file.css" rel="stylesheet" id="boxcss" runat="server" />
在后面的代码中:
string iframe = "iframe{border-radius:300px}";
boxcss.Attributes.Add("class",iframe);
感谢您的帮助
您可以使用以下代码将样式属性添加到现有 .class1
选择器或创建新的 .class2
选择器:
Style class1 = new Style();
class1.BackColor = Color.Orange;
Header.StyleSheet.CreateStyleRule(class1, null, ".class1");
Style class2 = new Style();
class2.BorderColor = Color.Red;
class2.BorderStyle = BorderStyle.Solid;
class2.BorderWidth = Unit.Pixel(5);
Header.StyleSheet.CreateStyleRule(class2, null, ".class2");
但是,样式 class 不允许访问 border-radius
。您可以在 Literal 控件的帮助下将该样式属性添加到 Header:
Literal iframeStyle = new Literal();
iframeStyle.Text = "<style>iframe { border-radius: 300px; }</style>";
Header.Controls.Add(iframeStyle);
注意:head
标记必须具有 runat="server"
属性才能在代码隐藏中访问:
<head runat="server">