Change BgColor of aspx page in code

Discussion in 'ASP .Net' started by Cliff Cavin, Jan 17, 2004.

  1. Cliff Cavin

    Cliff Cavin Guest

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

    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

    Thanks,,,,, Cliff
    Cliff Cavin, Jan 17, 2004
    1. Advertisements

  2. Cliff Cavin

    Jos Guest

    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):
    Jos, Jan 17, 2004
    1. Advertisements

  3. Cliff Cavin

    Cliff Cavin Guest

    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

    The search continues,,,,, Cliff
    Cliff Cavin, Jan 17, 2004
  4. Cliff Cavin

    Showjumper Guest

    Are you using code behind? You'll need to declare the body tag as
    Showjumper, Jan 18, 2004
  5. Cliff Cavin

    Cliff Cavin Guest

    Yes, I am using code-behind.

    How do I declare the body tag as HtmlGenericControl ?

    Thanks,,,, Cliff
    Cliff Cavin, Jan 18, 2004
  6. Cliff Cavin

    Cliff Cavin Guest

    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 Cavin, Jan 18, 2004
  7. Cliff Cavin

    MSFT Guest

    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.

    Microsoft Online Support

    Get Secure!
    (This posting is provided "AS IS", with no warranties, and confers no
    MSFT, Jan 19, 2004
  8. Cliff Cavin

    Cliff Cavin Guest

    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
    Cliff Cavin, Jan 19, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.