MasterPage -> Page -> MasterPage Code Access

Discussion in 'ASP .Net' started by =?Utf-8?B?QWxleCBNYWdoZW4=?=, Apr 10, 2006.

  1. Hi. I'm a little confused about the code that resides in the code-behind of a
    MasterPage and the code that resides in the code-behind of the actual pages
    that USE that MasterPage.

    I'm noticing, for example, that the Page_Load on the specific page executes
    before the Page_Load of its MasterPage. Is this right? But what I really want
    to understand is VARIABLE SCOPE, etc. between the two.

    For example: Is there a way for me to write code in the MasterPage which
    will access the objects, tags, etc. of the specific Pages that USE that
    MasterPage? So, can I write a function that lives with the MasterPage that
    will look into the specific page at load time and do things based on the
    tags, etc. on that page? If so, how?

    Alex
    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Apr 10, 2006
    #1
    1. Advertising

  2. You can access controls in the master page from the child page using the
    master.FindControl("controlID"); syntax. But you cannot access anything at
    all in the child page from the master page. If you want something to happen
    in the master page, depending on which child page is using it, put your code
    in the relevent child page and use the Master.FindControl("controlID");
    syntax outlined above. You can also expose controls and vairables etc of the
    master page as properties.

    "Alex Maghen" wrote:

    > Hi. I'm a little confused about the code that resides in the code-behind of a
    > MasterPage and the code that resides in the code-behind of the actual pages
    > that USE that MasterPage.
    >
    > I'm noticing, for example, that the Page_Load on the specific page executes
    > before the Page_Load of its MasterPage. Is this right? But what I really want
    > to understand is VARIABLE SCOPE, etc. between the two.
    >
    > For example: Is there a way for me to write code in the MasterPage which
    > will access the objects, tags, etc. of the specific Pages that USE that
    > MasterPage? So, can I write a function that lives with the MasterPage that
    > will look into the specific page at load time and do things based on the
    > tags, etc. on that page? If so, how?
    >
    > Alex
    =?Utf-8?B?Y2xpY2tvbg==?=, Apr 10, 2006
    #2
    1. Advertising

  3. Thanks for Clickon's inputs,

    Hi Alex,

    As for the ASP.NET 2.0's MasterPage, it is dynamically loaded into the
    concrete page which has been configured to apply that masterpage. You can
    think the masterpage like a Usercontrol which loaded into the page's
    control collection, the relationship is different from the class
    inheritance in OOP. Also, for your requirement on accessing object, data
    in concrete page through the MasterPage's codebehind code, I think it is
    certainly possible. Clickon has mentioned the means that we can query the
    controls through the FindControl method on page. And actually the "Page"
    property of the MasterPage reference to the instance of the concrete
    page(you can use the Page.GetType to verify this). So since we can get the
    concrete page instance reference in MasterPage, we can access public
    methods, properties (built-in or custom ones) in our master page's code.
    One problem remains is the Type Casting, since the Page property is of the
    System.Web.UI.Page class, if we want to access any custom property or
    methods in the concrete page class, we need to do the casting. However,
    it's not very convenient (and not good practice) to hardcoded concrete
    page's type and casting it. One good approach is defining a common
    interface which expose some properties or functions which will be useful to
    the MasterPage, then our concrete page can implement this interface and
    according to the detailed page structure or info. e.g:

    ========in master page==========
    public partial class App_Master_simple : System.Web.UI.MasterPage
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Response.Write("<br/>" + ((IHello)Page).SayHello("Steven"));
    }
    }


    ========the concrete page which applying the master page=====

    public partial class concretes_newConcretePage : System.Web.UI.Page, IHello
    {
    .................................

    #region IHello Members

    public string SayHello(string name)
    {
    return "Hello " + name + "<br/>" + TextBox1.Text;
    }

    #endregion
    }


    We can expose control info or other custom data/properties or operations
    in concrete page to master page through such interface.

    Hope this helps. If there is any other ideas or questions, please feel free
    to post here.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Apr 11, 2006
    #3
  4. Hi Alex,

    Does my further suggestion help a little? If there's anything else we can
    help, please feel free to post here.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Apr 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThunderMusic
    Replies:
    2
    Views:
    432
    Gregory Gadow
    Sep 27, 2006
  2. Randy Smith
    Replies:
    9
    Views:
    410
    Mark Rae
    Apr 14, 2007
  3. mr t
    Replies:
    0
    Views:
    387
  4. Torben Laursen
    Replies:
    4
    Views:
    567
    clintonG
    Mar 15, 2008
  5. Savvoulidis Iordanis
    Replies:
    2
    Views:
    797
    Alexey Smirnov
    Jun 30, 2009
Loading...

Share This Page