Access a content control in C# when using Master Pages

Posted by Guillaume Gervais on Stack Overflow See other posts from Stack Overflow or by Guillaume Gervais
Published on 2009-06-29T20:44:51Z Indexed on 2013/11/07 21:55 UTC
Read the original article Hit count: 288

Filed under:
|
|

Good day everyone,

I am building a page in ASP.NET, and using Master Pages in the process.

I have a Content Place Holder name "cphBody" in my Master Page, which will contain the body of each Page for which that Master Page is the Master Page.

In the ASP.NET Web page, I have a Content tag (referencing "cphBody") which also contains some controls (buttons, Infragistics controls, etc.), and I want to access these controls in the CodeBehind file. However, I can't do that directly (this.myControl ...), since they are nested in the Content tag.

I found a workaround with the FindControl method.

ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Master.FindControl("cphBody");
ControlType myControl = (ControlType) contentPlaceHolder.FindControl("ControlName");

That works just fine. However, I am suspecting that it's not a very good design. Do you guys know a more elegant way to do so?

Thank you!

Guillaume Gervais.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about master-pages