2012-02-02 66 views
3

我该如何设置服务器的css(background)属性?此刻:(如何从serverside asp.net设置css属性?

CS

public string MyBackgroundColor { get; set; } 

ASPX

<style type="text/css"> 
    html 
    { 
     background-color: '<%=MyBackgroundColor %>'; 
    } 
</style> 
+0

你是说这根本不会出现,或者它没有显示你所期望的颜色?你看到呈现的HTML中的值?我会删除一个单引号。 而且,你在某处分配一个值给MyBackgroundColor?你没有分享足够的代码。 – 2012-02-02 04:03:30

回答

6

不工作,你可以尝试

的.aspx

<body id="body" runat="server"> 
...the body 

.aspx.cs

body.Style["Background-Color"] = blue;//just example you can try using your method too 
0

假设你的风格标签是在<head>元素,改变=#background-color: '<%#MyBackgroundColor %>';)添加runat="server"<head>元素,在Page_Load事件调用Page.Header.DataBind()

2

你能做到这样

Page.Header.Controls.Add(
    new LiteralControl(
     @"<style type='text/css'> 
       html 
       { 
        background-color: red; 

       } 
       </style> 
      " 
     ) 
); 
0

yourPageOrControlInstance.Styles["background"] = "yellow blah-blah";