Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Do UserControls have acces to the container?

Reply
Thread Tools

Do UserControls have acces to the container?

 
 
Jenda Krynicky
Guest
Posts: n/a
 
      09-07-2004
Sorry I'm probably missing something basic, but can't find it in the docs.

Is it possible to access the public properties of the page containing the
UserControl from within the control? Without having to set the link up from
within the code of the page of course.

Most of my pages inherit not from System.Web.UI.Page, but from my own class.
The class has a Public property, an object containing info about the
currently logged in user. Some of the pages use a UserControl I made. The
control also needs access to the object (or needs to create its own based on
the info in Context.User.Identity).

I know I could add a public property "login" to the control and set it to
the created object from within the initialization code of the pages, I know
I could do it by just inheriting another template page from the one I
already have and inherit the pages that use this UserControl from the new
template, but I'd rather not. I think the control must have a reference to
its container already.

Thanks, Jenda


 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      10-21-2004
All web controls have a Page property. Just cast it to your derived type.

</joel>


"Jenda Krynicky" <> wrote in message
news:...
> Sorry I'm probably missing something basic, but can't find it in the docs.
>
> Is it possible to access the public properties of the page containing the
> UserControl from within the control? Without having to set the link up

from
> within the code of the page of course.
>
> Most of my pages inherit not from System.Web.UI.Page, but from my own

class.
> The class has a Public property, an object containing info about the
> currently logged in user. Some of the pages use a UserControl I made. The
> control also needs access to the object (or needs to create its own based

on
> the info in Context.User.Identity).
>
> I know I could add a public property "login" to the control and set it to
> the created object from within the initialization code of the pages, I

know
> I could do it by just inheriting another template page from the one I
> already have and inherit the pages that use this UserControl from the new
> template, but I'd rather not. I think the control must have a reference to
> its container already.
>
> Thanks, Jenda
>
>



 
Reply With Quote
 
 
 
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      10-22-2004
Joel wrote:
> All web controls have a Page property. Just cast it to your derived type.
>


While this is definitely true, it's rarely a good idea to have the User
Control reference up to its parent container in this manner. Ideally,
User Controls should be agnostic about their container. This would
allow them to be plugged in and out of various pages without having to
muck with the code. By tying the UC to its parent, you destroy any
hopes at encapsulation and reusability. (Assuming that the UC is
accessing properties/methods of the parent container that are not
inherent to the Page class.)

If it is essential that the UC interacts with the parent page, a better
approach would be to use an interface or delegates. (Again, assuming
that the UC is accessing something other than a property/method already
outlaid in the Page class...)




> "Jenda Krynicky" <> wrote in message
> news:...
>
>>Sorry I'm probably missing something basic, but can't find it in the docs.
>>
>>Is it possible to access the public properties of the page containing the
>>UserControl from within the control? Without having to set the link up

>
> from
>
>>within the code of the page of course.
>>
>>Most of my pages inherit not from System.Web.UI.Page, but from my own

>
> class.
>
>>The class has a Public property, an object containing info about the
>>currently logged in user. Some of the pages use a UserControl I made. The
>>control also needs access to the object (or needs to create its own based

>
> on
>
>>the info in Context.User.Identity).
>>
>>I know I could add a public property "login" to the control and set it to
>>the created object from within the initialization code of the pages, I

>
> know
>
>>I could do it by just inheriting another template page from the one I
>>already have and inherit the pages that use this UserControl from the new
>>template, but I'd rather not. I think the control must have a reference to
>>its container already.



--

Scott Mitchell

http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
Reply With Quote
 
Wessam Zeidan
Guest
Posts: n/a
 
      10-22-2004
I've solved this problem in this article

http://www.serversidecode.net/absolu...ates/?a=97&z=8
 
Reply With Quote
 
Jenda Krynicky
Guest
Posts: n/a
 
      10-22-2004
> While this is definitely true, it's rarely a good idea
> to have the User Control reference up to its parent
> container in this manner. Ideally, User Controls
> should be agnostic about their container. This would
> allow them to be plugged in and out of various pages
> without having to muck with the code. By tying the
> UC to its parent, you destroy any hopes at encapsulation
> and reusability. (Assuming that the UC is accessing
> properties/methods of the parent container that are
> not inherent to the Page class.)


This is all nice and good (and probably learned from some purist book)
but it does not apply in this case.

1) All pages except Login.aspx inherit from Projectname.TSPage instead
from System.Web.UI.Page. And I do not want to use something specific to
the individual pages, but a public property of the Projectname.TSPage.
2) Almost all pages in the project except Login.aspx use that user
control.
3) The user control is totaly application specific, it would have no
meaning whatsoever outside the app.

So thanks for your concern, Jenda


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      10-24-2004
Jenda Krynicky wrote:
>>While this is definitely true, it's rarely a good idea
>>to have the User Control reference up to its parent
>>container in this manner. Ideally, User Controls
>>should be agnostic about their container. This would
>>allow them to be plugged in and out of various pages
>>without having to muck with the code. By tying the
>>UC to its parent, you destroy any hopes at encapsulation
>>and reusability. (Assuming that the UC is accessing
>>properties/methods of the parent container that are
>>not inherent to the Page class.)

>
>
> This is all nice and good (and probably learned from some purist book)
> but it does not apply in this case.


It was learned the ol' fashion way - through experience.

> 1) All pages except Login.aspx inherit from Projectname.TSPage instead
> from System.Web.UI.Page. And I do not want to use something specific to
> the individual pages, but a public property of the Projectname.TSPage.


As I said, if the data you are referencing is specific to the base class
or interface of the page, then by all means the UC can go ahead and
access it. Here, let me reiterate where I said that:

>>(Assuming that the UC is accessing properties/methods of the
>>parent container that are not inherent to the Page class.)


> 3) The user control is totaly application specific, it would have no
> meaning whatsoever outside the app.


Components often have a wily way of needing their scope to be widened
after the fact. All I am saying is that in the future you may want to
use this UC in another project, only to find that you made some
design-time decisions that hinder its reusability.

--

Scott Mitchell

http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
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
Re: How do you acces ASP.NET WAT on a Web server that doesnt have VS o Juan T. Llibre ASP .Net 18 12-20-2006 02:09 AM
Acces controls on aspx page from usercontrols fabrice ASP .Net Web Controls 1 05-25-2005 09:38 AM
Ad Hoc or Acces Point Pete Kennedy Wireless Networking 8 04-21-2005 01:45 PM
Disabling wireless acces on a domain =?Utf-8?B?am9obl9iYWdlcg==?= Wireless Networking 1 01-12-2005 11:54 PM
Not able to acces authentication tab wireless networkconnection pr =?Utf-8?B?bm9vcmQ0NTM=?= Wireless Networking 3 10-15-2004 07:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57