Accessing master page properties from server.transfer page?

Discussion in 'ASP .Net' started by evantay, May 26, 2007.

  1. evantay

    evantay Guest

    I'm using ASP.NET 2.0 with VS.NET 2005. I'm trying to access properties
    from my master pages within a page that inherits from that master page (a
    child page). However the values are always null.

    In my masterpage I have this:

    private bool m_AlreadyTested;
    public bool AlreadyTested
    {
    get { return m_AlreadyTested; }
    set { m_AlreadyTested = value; }
    }

    Then within the Page_Load of the master page I am doing this:

    if certainConditions == true then
    AlreadyTested = true;
    Server.Transfer("/error.aspx");
    end if

    Then in error.aspx child page (derrived from the master page) I do this from
    its Page_Load event:
    Response.Write(Master.AlreadyTested.toString());

    However the AlreadyTested always displays as FALSE, even though itis hard
    coded to True. When I step through in the debugger I can see it is set to
    True just prior to the Server.Transfer. But once it gets into the transfered
    page everything is nulled out and bools are set to False.

    Similarly I have other properties I've set to valid object instances prior
    to the .Transfer and afterward those are all Null too.

    What is going on here? I thought under ASP.NET 2.0 I could tap into the
    master page properties even from exceution of the .Transfer pages?

    Thanks!
     
    evantay, May 26, 2007
    #1
    1. Advertising

  2. Sure you can access MasterPage properties, but what are you expecting to
    happen when you move to another page? First, what's going on here is you've
    just moved from one page to another with the server.transfer. This means
    that absolutely everything that was available before isn't because it's a
    completely new page. Your MasterPage isn't static, which could be where your
    confusion lies. If it was static than these properties would be acecssible
    globally, btu that's not the way things work. Each page that inherits from
    your masterpage is completely seperate and instanced. You will only gain
    access to the properties of the current instance of a masterpage. So, your
    property AlreadyTested will work within that particular page instance. Once
    you use Server.Transfer you're essentially doing a server-side redirect to
    another page, a new instance which includes a new set of variables and a
    blank slate. The variables cannot be maintained since this is a completely
    seperate page even though it inherits from a masterpage.

    --
    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006

    "evantay" <> wrote in message
    news:...
    > I'm using ASP.NET 2.0 with VS.NET 2005. I'm trying to access properties
    > from my master pages within a page that inherits from that master page (a
    > child page). However the values are always null.
    >
    > In my masterpage I have this:
    >
    > private bool m_AlreadyTested;
    > public bool AlreadyTested
    > {
    > get { return m_AlreadyTested; }
    > set { m_AlreadyTested = value; }
    > }
    >
    > Then within the Page_Load of the master page I am doing this:
    >
    > if certainConditions == true then
    > AlreadyTested = true;
    > Server.Transfer("/error.aspx");
    > end if
    >
    > Then in error.aspx child page (derrived from the master page) I do this
    > from
    > its Page_Load event:
    > Response.Write(Master.AlreadyTested.toString());
    >
    > However the AlreadyTested always displays as FALSE, even though itis hard
    > coded to True. When I step through in the debugger I can see it is set to
    > True just prior to the Server.Transfer. But once it gets into the
    > transfered
    > page everything is nulled out and bools are set to False.
    >
    > Similarly I have other properties I've set to valid object instances prior
    > to the .Transfer and afterward those are all Null too.
    >
    > What is going on here? I thought under ASP.NET 2.0 I could tap into the
    > master page properties even from exceution of the .Transfer pages?
    >
    > Thanks!
    >
    >
    >
     
    Mark Fitzpatrick, May 26, 2007
    #2
    1. Advertising

  3. evantay

    evantay Guest

    Thanks Mark. This makes perfect sense. Perhaps server.transfer then is not
    the ideal way to go about what I want...

    What I am really looking for is a way that I can "include" a page
    dynamically within the master page framework.

    For example let's say I have my master page mymaster.master. Inside
    mymaster.master I check logic that if it evaluates to True I want the user
    to get noservice.aspx instead of the real web page. Currently I can just
    make my master page so that it does a server.transfer to noservice.aspx if
    those conditions are met, and therefore every web page that derrives (child
    pages) from the master automatically enforce this logic. So far all is
    good.

    However once control is passed to noservice.aspx I want to be able to be
    able to access some objects that were created in the master page without
    having to recreate them. Its almost like what I want to do is to tell .NET
    to merge (i.e. in-line) noservice.aspx directly into the page its processing
    if my logic evaluates to true, rather than having it to a service.transfer.

    One thing I am doing currently that seems to work ok is that I am able to
    store some objects I want to persist across the page request into the
    Context of the request. When I access this from the server.transfer page
    all is still in tact.

    Do any work around or alternatives come to mind?

    Thanks!!

    "Mark Fitzpatrick" <> wrote in message
    news:O%...
    > Sure you can access MasterPage properties, but what are you expecting to
    > happen when you move to another page? First, what's going on here is
    > you've just moved from one page to another with the server.transfer. This
    > means that absolutely everything that was available before isn't because
    > it's a completely new page. Your MasterPage isn't static, which could be
    > where your confusion lies. If it was static than these properties would be
    > acecssible globally, btu that's not the way things work. Each page that
    > inherits from your masterpage is completely seperate and instanced. You
    > will only gain access to the properties of the current instance of a
    > masterpage. So, your property AlreadyTested will work within that
    > particular page instance. Once you use Server.Transfer you're essentially
    > doing a server-side redirect to another page, a new instance which
    > includes a new set of variables and a blank slate. The variables cannot be
    > maintained since this is a completely seperate page even though it
    > inherits from a masterpage.
    >
    > --
    > Hope this helps,
    > Mark Fitzpatrick
    > Former Microsoft FrontPage MVP 199?-2006
    >
    > "evantay" <> wrote in message
    > news:...
    >> I'm using ASP.NET 2.0 with VS.NET 2005. I'm trying to access properties
    >> from my master pages within a page that inherits from that master page (a
    >> child page). However the values are always null.
    >>
    >> In my masterpage I have this:
    >>
    >> private bool m_AlreadyTested;
    >> public bool AlreadyTested
    >> {
    >> get { return m_AlreadyTested; }
    >> set { m_AlreadyTested = value; }
    >> }
    >>
    >> Then within the Page_Load of the master page I am doing this:
    >>
    >> if certainConditions == true then
    >> AlreadyTested = true;
    >> Server.Transfer("/error.aspx");
    >> end if
    >>
    >> Then in error.aspx child page (derrived from the master page) I do this
    >> from
    >> its Page_Load event:
    >> Response.Write(Master.AlreadyTested.toString());
    >>
    >> However the AlreadyTested always displays as FALSE, even though itis hard
    >> coded to True. When I step through in the debugger I can see it is set
    >> to
    >> True just prior to the Server.Transfer. But once it gets into the
    >> transfered
    >> page everything is nulled out and bools are set to False.
    >>
    >> Similarly I have other properties I've set to valid object instances
    >> prior
    >> to the .Transfer and afterward those are all Null too.
    >>
    >> What is going on here? I thought under ASP.NET 2.0 I could tap into the
    >> master page properties even from exceution of the .Transfer pages?
    >>
    >> Thanks!
    >>
    >>
    >>

    >
    >
     
    evantay, May 29, 2007
    #3
  4. Hi evantay,

    You may find following articles useful:

    #Passing Server Control Values Between Pages
    http://msdn2.microsoft.com/en-us/library/6c3yckfw(vs.71).aspx

    #ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
    http://www.odetocode.com/Articles/450.aspx


    Regards,
    Walter Wang (, remove 'online.')
    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.
     
    Walter Wang [MSFT], May 30, 2007
    #4
  5. Hi evantay,

    You may find following articles useful:

    #Passing Server Control Values Between Pages
    http://msdn2.microsoft.com/en-us/library/6c3yckfw(vs.71).aspx

    #ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
    http://www.odetocode.com/Articles/450.aspx


    Regards,
    Walter Wang (, remove 'online.')
    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.
     
    Walter Wang [MSFT], May 30, 2007
    #5
    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. Doug McCann
    Replies:
    1
    Views:
    1,037
    William Brogden
    Aug 5, 2004
  2. =?Utf-8?B?U3VyZXNo?=

    Accessing Master Page Custom Properties in Design Mode

    =?Utf-8?B?U3VyZXNo?=, Oct 21, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    369
    =?Utf-8?B?U3VyZXNo?=
    Oct 21, 2006
  3. evantay
    Replies:
    0
    Views:
    374
    evantay
    May 26, 2007
  4. Leon Mayne

    Server.Transfer exception in master page

    Leon Mayne, Mar 4, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    516
    Leon Mayne
    Mar 4, 2008
  5. Jim
    Replies:
    1
    Views:
    903
    Aaron Bertrand - MVP
    Jan 28, 2004
Loading...

Share This Page