tags:

views:

34

answers:

4

Hi,

Easy one to explain. Is there any way I can do this:

<div id="header" style='<asp:Literal runat="server" ID="litBackgroundImage"></asp:Literal>' >

It looks valid, but Visual Studio will not recognise litBackgroundImage as a valid control in the code-behind. Setting the div as runat="server" won't work either because the style property is read-only.

Any suggestions gratefully received

+1  A: 

Add runat="server" to your div and then you can access it from server-side, so you can set the style attribute dynamically.

BlueCode
You can't set style dynamically, as I said above - it's read only.
Matt Thrower
@Matt, I think that BlueCode was talking about using the Attributes property instead of the Style property.
Tony Abrams
+1  A: 

This might work:

HtmlControl headerDiv = (HtmlControl)this.FindControl("header");
headerDiv.Attributes.Add("class", **NAMEOFCSSCLASSTOUSE**);

I haven't tested it, but it should work. You will have to make sure that your style is a css class in a css file.

EDIT

After testing the above won't work, but the following worked fine for me:

<div id="header3" runat="server">This is header3 ...</div>

and in the code-behind:

header3.Attributes("style") = "height: 100px; background-color: red;"
Tony Abrams
A: 

Are you nesting your Literal inside a <form> tag? It's an obvious answer but one worth mentioning.

AlexW
A: 

try this

<div id="div1" runat="server">...</div>

and

div1.Style["width"] = "100px";
div1.Style["color"] = "#FF0000";
y34h