Change BgColor of aspx page in code

  • Thread starter Thread starter Cliff Cavin
  • Start date Start date
C

Cliff Cavin

How do I programatically change the background color of an aspx page in
code?

Is there something like Page.BgColor = "#FFFFFF" ?

For that matter, how do I access any of the DOCUMENT properties in code?

I am using Visual Studio 2003, building an ASP.NET web application using
VB.NET.

Thanks,,,,, Cliff
 
Cliff said:
How do I programatically change the background color of an aspx page
in code?

Is there something like Page.BgColor = "#FFFFFF" ?

For that matter, how do I access any of the DOCUMENT properties in
code?

I am using Visual Studio 2003, building an ASP.NET web application
using VB.NET.

Thanks,,,,, Cliff

Add an id to the body tag of the page like this:
<body id="bodytag" runat="server">
This makes the body tag accessible from your code.

Then, use this code to set the color (for VB):
bodytag.Attributes("bgcolor")="green"
or
bodytag.Attributes("bgcolor")="#ffffff"
 
Thanks Jos,

But I could not get that to work.
I changed my <Body> tage to be:
<body id="PageBody" runat="server">

But, from with VB it says that PageBody is undeclared.
I suspect that is because the body tag is not within the main
<FORM id="Form1" method="post" runat="server"> tag.

I can not move the body tags to be within the form tag because then
it says "Per the active schema, the element 'FORM' cannot be nested within
'html'.

The search continues,,,,, Cliff
 
Yes, I am using code-behind.

How do I declare the body tag as HtmlGenericControl ?

Thanks,,,, Cliff
 
Finally! I got it.

To programatically, using VB Code Behind, change the background color at
Page_Load time, do this:

1) Bring up the HTML for the aspx page.
2) Change the <body> tag to <body id="PageBody" runat="server")
3) Bring up the code behind and place this code in the Page_Load event, or
actuall I guess it could go anywhere:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim MyBody As New HtmlGenericControl
MyBody = Page.FindControl("PageBody")
MyBody.Attributes("bgcolor") = "#FF00FF"
End Sub


Note: The Page.FindControl seems to to be the missing link.
Missing from all VS MSDN Library.
Missing from all newsgroup posts.

If you leave out the line: MyBody = Page.FindControl("PageBody")
the solution will still compile and run. It just won't change the
background color.

Unfortuately, the documentation that comes with VS is all from the point of
view that you are NOT using code behind. And there *are* a few small
differences between using code behind and not using code behind.

Well, there goes another 8 hours of my life researching something that
should be stupid simple.

I am guessing this same idea could be extended to manipulate any of the
otherwise unreachable DOCUMENT properties from within code (although I have
not had the courage to try yet).

Hope this helps the next guy with the same *simple* question.

Cliff
 
Hi Cliff,

To declare the body tag as HtmlGenericControl, you can just add follwong
line to the code behind:

Public Class WebForm1
Inherits System.Web.UI.Page

...

Private designerPlaceholderDeclaration As System.Object

...

Just like declaring a Button or Datagrid on the page.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
I wanted to add a note to this previous post.

If you have a Stylesheet attached to the page, the values in the stylesheet
will override the values you set with the code snipett below, giving the
appearance that it did not work.

Thanks,,,,, Cliff
 
Back
Top