简体   繁体   中英

Accessing a contentplaceholder on a master page

I am trying to access a content placeholder from Site.Master.cs code-behind. I tried the following:

ContentPlaceHolder cph = (ContentPlaceHolder)this.FindControl("MainContent");

and

ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("MainContent");

But it gives me an error and when I tried debugging and this.Controls has objects but not "MainContent"

How can I access the master page placeholder controls?

Since the ContentPlaceHolder is in the markup and the code behind you're modifying is for the same page, you can access it directly.

var cph = MainContent;

Of course, probably no need to assign it to a variable since it's already in scope.

MainContent.Visible = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM