Reference Controls in MasterPage from Class in App_Code

Last post 11-02-2007 12:40 PM by pmurray. 6 replies.

Sort Posts:

  • Reference Controls in MasterPage from Class in App_Code

    03-19-2006, 11:09 PM
    • Participant
      976 point Participant
    • clintonG
    • Member since 06-14-2002, 9:02 PM
    • Milwaukee County, Wisconsin, USA
    • Posts 238

    I toggle the Enable property of several LinkButtons located in a MasterPage and I want to move the code into a class file in App_Code.

    I know how to reference controls in the MasterPage from Content pages but I'm having a problem when trying to use the code in a class that references the controls in the Master. My class looks like this...

    // basic use case (base class inherits System.Web.UI.Page)
    public class HeaderTabbedNavigation : GlobalBaseClass
    {
      public void IsEnabled(bool enabled)
      {
           if(enabled == true)
           MemberLoginStatus.Enabled = true;
           return;
      }
    }

    // direct access (fails)
    HeaderTabbedNavigation.IsEnabled(true);

    // error
    'MemberLoginStatus' does not exist in the current context

    Is this enough information for somebody to tell me what am I doing wrong in this class and how I should start thinking when creating classes in this context?

    <%= clintonG
  • Re: Reference Controls in MasterPage from Class in App_Code

    03-21-2006, 10:01 AM
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 3:18 PM
    • Citizen of the Earth
    • Posts 1,228

    Clinton:

    I can't quite grasp the context here. Do you have a base class defined for your master page(s) also? Probably what you'll need to do is have a base class or interface in App_Code for your master page to derive from. The interface will have to define properties the master page has to implement. You could program against the master page using that interface when writing classes in App_Code.

    Just remember App_Code compiles before the rest of the web app, so it will never be able to know about any particular master page/form/user control. Inheritance is the key to working with a master page/form/user control from App_Code.

    Hope that is some help,

    Scott
    http://www.OdeToCode.com/blogs/scott/
  • Re: Reference Controls in MasterPage from Class in App_Code

    03-21-2006, 8:54 PM
    • Participant
      976 point Participant
    • clintonG
    • Member since 06-14-2002, 9:02 PM
    • Milwaukee County, Wisconsin, USA
    • Posts 238

    Ode-a-lay-eee-hooo :-)

    Thanks for showing up. The GlobalBaseClass I declared has no direct relationship with the MasterPage. Its the base class I use throughout the application to inherit from System.Web.UI.Page allowing me to use Page_PreInit in that base class to change Themes, the Profile and other operations that need to be processed before the control tree is initialized.

    I started thinking the Page class would be needed by the custom class and integrated with the MasterPage "user control" when the Page is compiled.

    So how would -- you -- design and implement a custom class that includes a method used to change display properties for a number of different Linkbutton controls which are located in the Master?

    This is basically a class used to change the visible properties of a group of LinkButton controls being used as "tabbed" navigation.

    I haven't found a blog that models how to design and implement a tabbed control yet. Not even that OdeToCode place as I recall ;-) 

     

    <%= clintonG
  • Re: Reference Controls in MasterPage from Class in App_Code

    03-21-2006, 10:42 PM
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 3:18 PM
    • Citizen of the Earth
    • Posts 1,228

    :)

    Well, I'm actually going to be working on an article over the next two weeks that should touch on this subject.

    What if you had your master page(s) inherit from a base class in App_Code. It would look something like the following (I'm going to demonstrate with a Label control, because I don't have a tabbed navigation thingy :)). The concept works the same - you'll just need to add properties for your special navigation controls.

    Anyway - let's say I have the following in App_Code:

     

    public class MasterBase : MasterPage
    {

        
    // make sure your control field is marked as protected, not private.
        protected Label _footer;

        
    public Label FooterLabel
        {
            
    get { return _footer; }
            
    set { _footer = value; }
        }
       

    }

    My master page would look something like:

    <%@ Master ... Inherits="MasterPage" CodeFileBaseClass="MasterBase" %>

    ...

      
    <asp:Label ID="_footer" runat="server" />

     

    Make sure to set the CodeFileBaseClass so the runtime can wire up the _footer control in the markup with the protected _footer control in your base class. The code-beside for the master page just needs to derive from the MasterBase

    public partial class MasterPage : MasterBase
    {
       // ...
    }

    Now, if I have a PageBase class in App_Code, it can see the MasterBase Type, and it can cast it's Master property to that type and fiddle with any of the properties defined on MasterBase.

    public class PageBase : Page
    {
        
    protected void DoSomethingWithLabel()
        {
            
    MasterBase master = Master as MasterBase;
            master.FooterLabel.Text =
    "Hello, I am a label";
        }
    }

    So now I'm changing the properties of my master pages from a base class in App_Code. Basically I'd have the controls as properties on my base master page class.

    Is that helpful at all?

    Scott
    http://www.OdeToCode.com/blogs/scott/
  • Re: Reference Controls in MasterPage from Class in App_Code

    03-22-2006, 11:29 AM
    • Participant
      976 point Participant
    • clintonG
    • Member since 06-14-2002, 9:02 PM
    • Milwaukee County, Wisconsin, USA
    • Posts 238
    Great plan. I hadn't even thought about this approach yet and I'll see what I can do with it. Thank you for your insight...
    <%= clintonG
  • Re: Reference Controls in MasterPage from Class in App_Code

    11-02-2007, 12:29 PM
    • Member
      4 point Member
    • pmurray
    • Member since 11-02-2007, 11:43 AM
    • Posts 7

    I am attempting to implement this code example but using nested master pages. Here are some code snippets:

    Parent master page - gm.master.cs

    public partial class gm : gmMasterPage {}

    gm.master

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="gm.master.cs" CodeFileBaseClass="gmMasterPage" Inherits="gm" %>

    and in the body:

    <asp:Label ID="lblTagLine" runat="server" CssClass="tagLine" Text="Serving You"></asp:Label>

    gmMasterPage.cs in App_Code directory:

    public class gmMasterPage : MasterPage {

    protected Label lblTagLine;

    public gmMasterPage() {}

    public Label TagLine {

    get { return lblTagLine; }set { lblTagLine = value; }

    }

     

    }

    The childMasterPage also inherits from gmMasterPage but does not have a lblTagLine:

    cMenu.master.cs: 

    public partial class cMenu : gmMasterPage

    cMenu.master: 

    <%@ Master Language="C#" AutoEventWireup="true" MasterPageFile="~/gm.master" CodeFile="cMenu.master.cs" Inherits="cMenu" %>

    Base class page:

    public partial class gmPage : System.Web.UI.Page {

    protected void setTagLine(string tagLine)

    {

    ((
    gmMasterPage)Master).TagLine.Text = tagLine;

    }

    }

    And the page:

    in the myPage.aspx @Page directive -

    CodeFileBaseClass="gmPage"

    myPage.cs:

    public partial class myPage : gmPage {

    protected void Page_Load(object sender, EventArgs e) {

    setTagLine("Serving you locally...");

    }

    }

    When I attempt to run this code, the Page_Load calls the gmPage code behind which in turn gets the gmMasterPage lblTagLine object but then when it attempts to set the .Text of that Label, I get a null reference exception stating that the Label has not been instantiated.

    I would appreciate any suggestions - I have 3 text areas that I would like to have in the parent master page which are set by the page. I would prefer that every page is required to set the three values, but that is not required.

     Thanks in advance for your help -

  • Re: Reference Controls in MasterPage from Class in App_Code

    11-02-2007, 12:40 PM
    • Member
      4 point Member
    • pmurray
    • Member since 11-02-2007, 11:43 AM
    • Posts 7

    I found the problem, and "a" fix. The issue is that the setTagLine identified the Master page and attempted to set it's label - but the label actually existed in the ParentMaster page and the reference to gmMasterPage myMaster pointed to the cMenu.master. Here is what I changed the code to:

    protected void setTagLine(string tagLine)

    {

    gmMasterPage myMaster = Master as gmMasterPage;

    gmMasterPage myMastersMaster = myMaster.Master as gmMasterPage;

    myMastersMaster.TagLine.Text = tagLine;

    }

     If anyone knows a cleaner or better way, I'm happy to hear suggestions - but at least this WORKS!

Page 1 of 1 (7 items)