troubleshooting Question

Accessing Controls on a Master page from a class in the app_code folder

Avatar of djcheeky
djcheekyFlag for United States of America asked on
.NET ProgrammingC#ASP.NET
8 Comments1 Solution395 ViewsLast Modified:
Hi,

I am trying to get a handle on a control (a contentPlaceHolder) on one of my master pages.

So I did a bit of research and foun out that you can't actually access it directly from a class in the app_code folder to them being compiled in different assemblies.

What I read is that you need to create a base class and derive that from the MasterPage class.

I'm not really sure how to do this???

If you look at the code below (which is in a class called ErrorHandler.cs in app_code) you will see a reference to Master. This is not allowed and throws an error saying I don't have access to it.

How do I get access to this master page via my class??

Thanks


public void SetErrorContent(string contentPlaceHolderName, string controlName)
    {
        ContentPlaceHolder myContentPlaceHolder;
        Label myLabel;
        myContentPlaceHolder = (ContentPlaceHolder)Master.FindControl(contentPlaceHolderName);
 
       if (myContentPlaceHolder != null)
        {
           myLabel = (Label)myContentPlaceHolder.FindControl(controlName);
           if (myLabel != null)
           {
                myLabel.Text = "Label found!";
          }
        }
     }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros