asp.net changing Master Page section css from Content page

泪湿孤枕 提交于 2019-12-06 07:11:44
Tim Schmelter

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"%>

Now this works directly:

protected void Page_Init(object sender, EventArgs e)
{
    this.Master.SectionCssClass = "content-wrapper-full-width main-content clear-fix";
}

I think you could use the FindControl() method from code behind with something like :

((HtmlGenericControl)Master.FindControl("FeaturedContent")).attributes["class"] = "content-wrapper-full-width main-content clear-fix";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!