Do UserControls have acces to the container?

J

Jenda Krynicky

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
 
S

Scott Mitchell [MVP]

Joel said:
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...)





--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
J

Jenda Krynicky

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
 
S

Scott Mitchell [MVP]

Jenda said:
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:
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
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top