3

I have asp.net page like this:

Default.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

     ...

</asp:Content>

Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e) {
    BodyContent.Style["background-color"] = "Red";  // #1 This code doesn't work
}

I want to modify the background color of the page. I put the code above (#1) and it doesn't work.

So, how to modify the background color of the page?

3 Answers 3

6
   <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
         <div runat="server" ID="divMain">

</div>

    </asp:Content>
protected void Page_Load(object sender, EventArgs e) {

divMain.Style.Add("background-color", "red");
}

OR YOU CAN DO IT IN THIS WAY

       Page.Header.Controls.Add(
    new LiteralControl(
        @"<style type='text/css'>
                .myClass
                {
                    background-color: red;

                }
                </style>
            "
        )
);
Sign up to request clarification or add additional context in comments.

12 Comments

The background color changed only for the divMain, but not the entire page (<body>).
Page.Header.Controls.Add( new LiteralControl( @"<style type='text/css'> body { background-color: red; } </style> " ) );
@ Ashfaq Shaikh: oops, yes the second method works. Is there other alternative? Thx
Actually, I set the background colour based on the value saved in cookie. So your code will be like this: Page.Header.Controls.Add( new LiteralControl( @"<style type='text/css'>body { background-color: " + Request.Cookies["BackgroundColor"].Value + "; }</style>" ) );
you can make body tab as runat server and give it class. it will works fine.
|
5

You can't style Content controls, as they don't render as any HTML element. They are just used to separate the content between files.

You can put a style element in the header:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<style type="text/css">
body { background-color: red; }
</style>
</asp:Content>

1 Comment

Yes, it works but I'm looking for the codebehind solution to set the background-color.
3

Wrap a DIV around your contentplaceholder in your masterpage, and use CSS to style the DIV.

This will result in

<div class="mydiv">
<asp:ContentPlaceHolderID="MainContent"> 
     ... 
</asp:Content>
</div>

1 Comment

When I try this, I receive error: Parser Error Message: Only Content controls are allowed directly in a content page that contains Content controls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.