Avatar of djcheeky
djcheekyFlag for United States of America asked on

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

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!";
          }
        }
     }

Open in new window

.NET ProgrammingASP.NETC#

Avatar of undefined
Last Comment
djcheeky

8/22/2022 - Mon
raterus

I think you're getting confused between two different topics.  Perhaps it would be best to tell me what you are trying to do, and I'll suggest a better method to do what you're after.
ASKER
djcheeky

OK, cool haha!!

 I want to set the text value of a Label located on a masterpage from a class, ErrorHandler.cs (located in app_code).

As you can see by the code posted above, the functionionality to do this is pretty straightforward.
But the code I have posted above is taken from a code-behind file, not a class. What I am doing is using this code within a class.

The problem with this is, unlike the code-behind file which has access to Master, I don't have access to the master page from the errorHandler class.

How do I modify the value of value on a master page from a class?

Thanks,



ASKER CERTIFIED SOLUTION
raterus

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
djcheeky

Ok - you have two seperate solutions posted above...

>> "Could you just put the code in an easier spot (like the code file of the master page?)"

Is this possible? I thought code-behind files could only contain evert handlers? Do you mean I could add the SetErrorContent() function to my masterpage.cs file and simply call it from my pages?

>> "If you really must have this code separated, you can revisit your first thought about creating a base class for your masterpage, you can do it like this: in app_code, create a new class "MyAppMaster""

I have already created a class like your 'MyAppMaster' class above.
The part I'm not sure about, where you say "Then in the code for your regular master, do the following replacement - public class SomeMaster : MyAppMaster"
Where do you do this class declaration?? In the masterpage.cs file??

Thanks for the help
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
raterus

The answers to your questions are both "Yes".  I wouldn't mess with a base page unless you have to at this point, just put the code you need in masterpage.cs
ASKER
djcheeky

Ok great - I'm at home now - but will give the solution a bash first thing in the morn and respond then!
Thanks raterus - appreciate it!
ASKER
djcheeky

Hi Raterus.

Ok, I have looked at the first option mentioned and I'm a bit stumped.

I have a master page code behind file called MyMaster.cs
This file has a SetErrorContent function within the class.

Now, how do I, from another normal code-behind file, access this master page??
Ultimately I'm trying to do something like simply call MyMaster.SetErrorContent() but it doesn't seem as simple as this? Any suggestions how to access this method??

Thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
djcheeky

Hi again.

Ok, I figured out that I need the following piece of code in each content page in order to reference the Master Page:

<%@ MasterType VirtualPath="~/myMasterPage.master" %>

Thanks for your help - the first option works as you said.