如何从内容页面后面的代码更改母版页的背景?

时间:2023-02-15 21:15:20

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.

我特别想要从使用该母版页的内容页面的代码(C#)中将背景颜色的样式添加到母版页的标记中。

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.

内容页面的页面加载功能。但我无法让上述工作线上班。我只需要更改页面的标签的背景颜色。

3 个解决方案

#1


8  

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:

II。在您的内容aspx页面中,使用寄存器注册MasterPage:

<% MasterPageFile="..." %>

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

III。在内容页面中,您现在可以简单地使用

Master.FindControl("masterpageBody")

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

并有权访问控件。现在,您可以更改您喜欢的任何属性/样式!

#2


1  

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

Where

哪里

default_body = the id of the body tag.

default_body =正文标记的id。

#3


0  

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. 允许页面/内容定义额外的自定义样式表或
  3. Allow a page/content to define inline style tags
  4. 允许页面/内容定义内联样式标记

#1


8  

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:

II。在您的内容aspx页面中,使用寄存器注册MasterPage:

<% MasterPageFile="..." %>

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

III。在内容页面中,您现在可以简单地使用

Master.FindControl("masterpageBody")

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

并有权访问控件。现在,您可以更改您喜欢的任何属性/样式!

#2


1  

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

Where

哪里

default_body = the id of the body tag.

default_body =正文标记的id。

#3


0  

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. 允许页面/内容定义额外的自定义样式表或
  3. Allow a page/content to define inline style tags
  4. 允许页面/内容定义内联样式标记