RE: Accessing Parent Page Members from a User Control

  • Thread starter Steven Cheng[MSFT]
  • Start date
S

Steven Cheng[MSFT]

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
 
G

Guest

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
 
S

Steven Cheng[MSFT]

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
 
S

Steven Cheng[MSFT]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top