2012-02-02 87 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";