c# - Wie kann ich den Hintergrund einer Masterseite gegenüber dem Code hinter einer Inhaltsseite ändern?

original title: "c# - How can I change the background of a masterpage from the code behind of a content page?"


I specifically want to add the style of background-color to the <body> tag of a master page, from the code behind (C#) of a content page that uses that master page.

I have different content pages that need to make the master page has different colors depending on which content page is loaded, so that the master page matches the content page's theme.

I have a solution below:

I'm looking for something more like:

Master.Attributes.Add("style", "background-color: 2e6095");

Inside of the page load function of the content page. But I can't get the above line to work. I only need to change the background-color for the <body> tag of the page.

Ich möchte speziell den Stil der Hintergrundfarbe zum hinzufügen <body> Tag einer Masterseite aus dem Code hinter (C #) einer Inhaltsseite, die diese Masterseite verwendet. Ich habe unterschiedliche Inhalte ...

Dies ist die Zusammenfassung nach der Übersetzung. Wenn Sie die vollständige Übersetzung anzeigen möchten, klicken Sie auf das Symbol "Übersetzen"

Alle Antworten
  • Translate

    What I would do for the particular case is:

    i. Define the body as a server side control

    <body runat="server" id="masterpageBody">

    ii. In your content aspx page, register the MasterPage with the register:

    <% MasterPageFile="..." %>

    iii. In the Content Page, you can now simply use


    and have access to the control. Now, you can change whatever properties/style that you like!

  • Translate

    This is what I came up with:

    In the page load function:

    HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("default_body");
    body.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#2E6095");


    default_body = the id of the body tag.

  • Translate

    I believe you are talking about a content management system. The way I have delt with this situation in the past is to either:

    1. Allow a page/content to define an extra custom stylesheet or
    2. Allow a page/content to define inline style tags