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

<p>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.</p>

<p>I have a solution below:</p>

<hr>

<p>I'm looking for something more like:</p>

<pre><code>Master.Attributes.Add("style", "background-color: 2e6095");
</code></pre>

<p>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 <code>background-color</code> for the <code>&lt;body&gt;</code> tag of the page.</p>

[{'answer_id': 2212, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 0, 'selected': False, 'text': '<p>I believe you are talking about a content management system. The way I have delt with this situation in the past is to either:</p>\n\n<ol>\n<li>Allow a page/content to define an extra custom stylesheet or</li>\n<li>Allow a page/content to define inline style tags</li>\n</ol>\n'}, {'answer_id': 2233, 'author': 'Adhip Gupta', 'author_id': 384, 'author_profile': 'https://Stackoverflow.com/users/384', 'pm_score': 4, 'selected': True, 'text': '<p>What I would do for the particular case is:</p>\n\n<p>i. Define the body as a server side control</p>\n\n<pre><code>&lt;body runat="server" id="masterpageBody"&gt;\n</code></pre>\n\n<p>ii. In your content aspx page, register the MasterPage with the register:</p>\n\n<pre><code>&lt;% MasterPageFile="..." %&gt;\n</code></pre>\n\n<p>iii. In the Content Page, you can now simply use </p>\n\n<pre><code>Master.FindControl("masterpageBody")\n</code></pre>\n\n<p>and have access to the control. Now, you can change whatever properties/style that you like!</p>\n'}, {'answer_id': 2291, 'author': 'Bryan Denny', 'author_id': 396, 'author_profile': 'https://Stackoverflow.com/users/396', 'pm_score': 1, 'selected': False, 'text': '<p>This is what I came up with:</p>\n\n<p>In the page load function:</p>\n\n<pre><code>HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("default_body");\nbody.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#2E6095");\n</code></pre>\n\n<p>Where </p>\n\n<blockquote>\n  <p>default_body = the id of the body tag.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/2209', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/396/']