Setting attributes of webform from source code

  • Thread starter Thread starter Newton
  • Start date Start date
N

Newton

Hi,

I am working with ASP.NET using C#. I put some labels,
buttons and editboxes on a webform. But i need how to
change a background color of the webform from the source
code. In windows application it is easy, because I can
call name of that form. But how is it in web
application ?

Somebody help me please,

thank you

Peter
 
OK, so it's slightly more complicated with WebForms to do this dynamically,
but anything that can be done in HTML can be done with ASP.NET, so let's get
to work.

First, set up your body tag to run in the server:

<body runat="server" id="myBody">
....
</body>

Now, you can set up a declaration of that in your codebehind:

protected HtmlGenericControl myBody;

Because you are using the generic control, you don't get direct access to
the bgcolor attribute, so you have to use the generic implementation in your
Page_Load event:

myBody.Attributes.Add("bgcolor", "#ffffcc");

There may be an easier way, perhaps, but this will get the job done.
 
Back
Top