Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

MasterPages and App_Code

Back to Forum: Tech Off
  • User profile image

    Hello everyone!

    Can anybody tell whether is is possible to access some element on the master page from the class located in App_Code and how?

    Let's say i have <asp:Label id="myLabel" runat="server" /> on the main master page + I have myClass.cs located in App_Code.

    In the code of the master page I write something like:

    private myClass objMyClass = new myClass();

    How to access myLabel from initiated objMyClass? I presume I should transfer the current context handle to the class, may be not...

    Thank you very much in advance.

  • User profile image

    If you do not inherit from System.Web.UI.Control then you need to pass a reference to the Page object ( this or me ). Otherwise use the Page property provided by your parent.

    To get reference to the MasterPage you need to traverse to it. I say that because MasterPages can inherit from MasterPages. So use the Master (I believe don't have VS in front of me) to access the Master of a current Page. It will aslo have the same property for it's Master. If the Master is null then you are at the top of your hierachy.

    Hope this helps.


  • User profile image

    Depending on what you want to do, it can be tricky. The code inside App_Code compiles into an assembly before any of the web forms or user controls. All of the web form, master page, and user control code will reference the App_Code assembly and not vice versa, so you can't interact with the master page in a stronly typed manner unless you define a base class in App_Code for your master page to derive from, or an interface for your master page to implement.

    I have an example showing how to access a user control via an interface defined in App_Code, you could do something similar for the master page.

    Making any sense?

  • User profile image

    Thank you very much bitmask for the reference! Looks usefull.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.