Access a user control properties in a master page

I am stuck on this. I migrated a .Net 1.1 web application to .Net 2.0 and I want to take advantage of the Masterpage model. I don’t want to rewrite everything so I want to keep some user controls (like headers and footers) embedded in the Master page. However I don’t get the right way to set from my code (VB.Net) the public properties of my user control. Imagine something like this: Masterpage: <%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %><%@ Register TagPrefix="Myapp" TagName="Footer" Src="~/Includes/Footer.ascx" %>…. <body><Myapp:Footer id=”Footer1” ShowDetails=”false” runat=server /></body> 
Footer.ascx is the user control where I define a public property ShowDetails, straightforward until there!Now if I create an aspx page with the master page, how from this page can I change the property ShowDetails? (something I do easily with ASP.Net 1.1)

I tried of course to cast the user control but unless some use of Reflection I don't see how I can type the control.

Any idea?

 

6 Comments

  • The funny thing is, someone asked me right about the same thing recently. In the article above I describe a technique to access Master page properties. Accessing user control properties in your master page would involve creating a few wrapper properties in the master page for accessing user control properties, and I would recommend using the interface technique mentioned in my article

    Is this what you are looking for?

  • Thanks Joost. But in your comment you forgot the link to the article!

    What I am looking for is somthing as simple as possible. Any chance you can post an example?

  • Create a class called basePage that inherits from System.Web.UI.Page

    have all your content pages inherit this 'super page' class.

    in the basepage class define methods that access/activate the controls within the master page.

    for example, to call a function in the footer i would add this to the basepage....

    protected sub SetFooterText(s as string)

    dim ftr as footer = me.master.findcontrol"myFooter")
    ftr.SetText(s)

    end sub

    This is how i do it and it works a treat.

  • help.net: I believe the link in his name takes you to the article he's referring to.

  • The page you're on has a reference to its master page (as a generic MasterPage), so I think you can cast that to your specific master page, then call public methods etc. on there. So...

    MyMasterPage m = Page.Master as MyMasterPage;
    if (m != null)
    {
    // call some public methods
    m.UpdateUserControl();
    }

    So, write the code to handle whatever you're doing in a public method on the master page and then call it from your aspx page.

  • Thanks Marcus I will have a try!

Comments have been disabled for this content.