May 19, 2010

Change CSS of a ASP.NET control at runtime. (C#)

Let's add background color and width  to a Label control at runtime.
(Runtime means on button click event or something like that).

Label1.Attributes.CssStyle.Add("background-color", "#007700");
Label1.Attributes.CssStyle.Add("width", "320px");

1 comment:

  1. We can only change the CSS of a control at runtime if it is a ASP.NET Sever Control, having id and the runat="Server"

    We can do this using follwoing method also.
    Label1.Style("width") = "320px"

    This method will be good in case the control has already "width" property assigned with some different value. It is also good if the control don't have that property set at all.

    ReplyDelete