asp.net从“内容"页面更改“母版页"部分的css [英] asp.net changing Master Page section css from Content page
问题描述
我的主页上有以下代码:
I have the following code in my master page:
<div id="body" runat="server">
<asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
<section runat="server" id="sectionMainContent" class="content-wrapper main-content clear-fix">
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</section>
</div>
对于一个特定的内容页面,我想将上面的<section>
的类值更改为class="content-wrapper-full-width main-content clear-fix"
For one specific content page I would like to change the class value of the <section>
above to something like class="content-wrapper-full-width main-content clear-fix"
如何从内容页面的代码后面访问<section>
的属性并修改其值?
How can I access the <section>
's attributes from codebehind of the content page and modify its value?
推荐答案
您可以在母版中创建一个公共属性,以获取/设置该类:
You can create a public property in your master which gets/sets the class:
// sectionMainContent is a HtmlGenericControl in codebehind
public String SectionCssClass
{
get { return sectionMainContent.Attributes["class"]; }
set { sectionMainContent.Attributes["class"] = value; }
}
现在,您可以将母版转换为正确的类型,并在内容页中访问此属性:
Now you can cast the master to the correct type and access this property in your contentpage:
protected void Page_Init(object sender, EventArgs e)
{
SiteMaster master = this.Master as SiteMaster; // replace with correct type
if(master != null)
master.SectionCssClass = "content-wrapper-full-width main-content clear-fix";
}
Side-note: you can use the @Master
directive to use the Master
property in your content-page strongly typed. Then you have compile time safety and you don't need to cast it to the actual type:
在内容页中(替换为实际类型):
In your content-page(replace with actual type):
<%@ MasterType VirtualPath="~/Site.Master"%>
现在这可以直接工作:
protected void Page_Init(object sender, EventArgs e)
{
this.Master.SectionCssClass = "content-wrapper-full-width main-content clear-fix";
}
这篇关于asp.net从“内容"页面更改“母版页"部分的css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!