null
null

Reputation: 9134

How to modify the style of asp.net page that use asp:Content?

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?

Upvotes: 3

Views: 15668

Answers (3)

Pleun
Pleun

Reputation: 8920

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>

Upvotes: 3

Ashfaq Shaikh
Ashfaq Shaikh

Reputation: 1668

   <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>
            "
        )
);

Upvotes: 6

Guffa
Guffa

Reputation: 700342

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>

Upvotes: 5

Related Questions