RE: Accessing Parent Page Members from a User Control

Discussion in 'ASP .Net' started by Steven Cheng[MSFT], Apr 6, 2004.

  1. Hi,

    Based on my understanding , you have defined a BasePage class(MyPage) and
    most other web pages in your asp.net web app will derived from it. Also,
    you have a UserControl which is used in some pages. And now you're
    wondering how to referece the container Page in the UserControl and access
    the page's Parent class's certain members, yes?

    As for this problem, I think you can just make use of the UserControl's
    "Page" member, this is the instance reference of the Page which contains
    the UserControl, you can create the BasePage's variable and convert the
    UserControl's Page member to the "BasePage" class and access its certain
    members, for example, in the UserControl's Page_Load event, we can use the
    following code:

    private void Page_Load(object sender, System.EventArgs e)
    {
    BasePage bp = (BasePage)this.Page;
    //set the base page's members
    bg.XXX = XXX;
    }

    Does this answer your questions?
    Thanks.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Apr 6, 2004
    #1
    1. Advertising

  2. Steven

    This *looks* as if it's the right way to go. I'm just coming across a little problem:

    The "BasePage" class that you show in your sample below is defined is a separate .CS file in my application and "BasePage" is inside a namespace (let's call the namespace "MyCompany"). When I do what you describe below, I get an compilation error, "The type or namespace name 'BasePage' could not be found". But since the .CS file which defines BasePage is not pre-compiled but it, rather used by all the ASPXs on the site with a
    <%@ Page Language="C#" Inherits="MyCompany.BasePage" Src="/Shared/Lib/BasePage.cs" %

    How can I do a "using" or whatever so that "BasePage" will be known/defined

    Help! Thanks

    ----- Steven Cheng[MSFT] wrote: ----

    Hi

    Based on my understanding , you have defined a BasePage class(MyPage) and
    most other web pages in your asp.net web app will derived from it. Also,
    you have a UserControl which is used in some pages. And now you're
    wondering how to referece the container Page in the UserControl and access
    the page's Parent class's certain members, yes

    As for this problem, I think you can just make use of the UserControl's
    "Page" member, this is the instance reference of the Page which contains
    the UserControl, you can create the BasePage's variable and convert the
    UserControl's Page member to the "BasePage" class and access its certain
    members, for example, in the UserControl's Page_Load event, we can use the
    following code

    private void Page_Load(object sender, System.EventArgs e

    BasePage bp = (BasePage)this.Page
    //set the base page's member
    bg.XXX = XXX


    Does this answer your questions
    Thanks


    Regards

    Steven Chen
    Microsoft Online Suppor

    Get Secure! www.microsoft.com/securit
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.

    Get Preview at ASP.NET whidbe
    http://msdn.microsoft.com/asp.net/whidbey/default.asp
     
    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Apr 6, 2004
    #2
    1. Advertising

  3. Hi ,

    From the further infos provided in your reply, it seems that your scenario
    is much more complex than I've expected :). One thing should be confirmed
    is that we can use the "using" directive to import a certain namespace or
    class only if we have referecned the certain class's assembly. So the
    "BasePage" you import in the Util Class need to be contained in a compiled
    assembly rather than a uncompiled cs source file. As for your situation, I
    think you shoud implement the classes refernece relation as below:

    Define a BasePage and compiled it in an assembly so that the Util class can
    reference it, then provide another page's code-behind file for those pages
    which contains another class derived from the precompiled "BasePage" Class.
    For example:
    <%@ Page Language="C#" Inherits="MyCompany.SourcePage"
    Src="/Shared/Lib/SourcePage.cs" %>

    The pageclass in the SourcePage.cs is derived from the "BasePage" Class
    which is in an precompiled assembly. Thus, the problem will be overcomed.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Apr 6, 2004
    #3
  4. Hi Alex,

    Have you had a chance to check the suggestions in my last reply or have you
    got any further ideas on this issue? If you have anything unclear or if
    there're anything else I can help, please feel free to post here. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Apr 8, 2004
    #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. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    600
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  2. Roy
    Replies:
    1
    Views:
    888
    Scott Allen
    Feb 10, 2005
  3. Suzanne Vogel
    Replies:
    5
    Views:
    2,393
    Dan W.
    Dec 9, 2003
  4. Dave
    Replies:
    19
    Views:
    342
    David A. Coursey
    Jan 14, 2005
  5. Kiran More
    Replies:
    2
    Views:
    339
    Kiran More
    Nov 14, 2006
Loading...

Share This Page