我的母版页中有以下代码:
<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"
我怎样才能访问<section>
内容页面代码隐藏中的属性并修改其值?
您可以在您的 master 中创建一个公共属性来获取/设置该类:
// sectionMainContent is a HtmlGenericControl in codebehind
public String SectionCssClass
{
get { return sectionMainContent.Attributes["class"]; }
set { sectionMainContent.Attributes["class"] = value; }
}
现在您可以将 master 转换为正确的类型并在内容页中访问此属性:
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";
}
旁注:您可以使用@Master指示 http://msdn.microsoft.com/en-us/library/vstudio/ms228274%28v=vs.100%29.aspx使用Master
内容页面中的属性是强类型的。然后你就拥有了编译时安全性,并且不需要将其转换为实际类型:
在您的内容页面中(替换为实际类型):
<%@ MasterType VirtualPath="~/Site.Master"%>
现在这可以直接工作:
protected void Page_Init(object sender, EventArgs e)
{
this.Master.SectionCssClass = "content-wrapper-full-width main-content clear-fix";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)