Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > RE: Accessing Parent Page Members from a User Control

Reply
Thread Tools

RE: Accessing Parent Page Members from a User Control

 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-06-2004
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




 
Reply With Quote
 
 
 
 
=?Utf-8?B?QWxleCBNYWdoZW4=?=
Guest
Posts: n/a
 
      04-06-2004
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





 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-06-2004
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

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-08-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Accessing USER CONTROL which is inside Masterpagethrough Another USER Control inside normal page. Kiran More ASP .Net Web Controls 2 11-14-2006 12:58 PM
Page load of the parent page called twice when a modal dialog is opened from a button click of the user control on the parent page Samy ASP .Net 2 08-15-2005 03:30 PM
Accessing a parent web form control from within a nested user control? Roy ASP .Net 1 02-10-2005 07:09 PM
Accessing members of a user control from the aspx page. Dave ASP .Net Web Controls 19 01-14-2005 04:51 PM



Advertisments