Tales from the Evil Empire

Bertrand Le Roy's blog

News

Ads Via DevMavens

ASP.NET AJAX UpdatePanel Control: Add Ajax interactivity to your ASP.NET 2.0 web pages


follow bleroy at http://twitter.com

Bertrand Le Roy




Add to Technorati Favorites

Blogs I read

My other stuff

Why you shouldn't expose public properties from your pages

We often have users asking us how they can access some variable that's in their page class from their user or custom controls.
The answer is that your page class can expose public properties, and then any control can cast its Page property to your specific Page-inherited class and gain access to the new properties.
But the second half of the answer is that you should really not do that even though it's possible.
There is a double reason for that.
The first is that it's your page that should orchestrate your controls (by accessing their properties and methods), not your controls orchestrating your page.
And the second, which is very close, is that your controls should not depend on your page implementing special properties or methods or containing specific controls. Otherwise, you're breaking one of the most important qualities of WebControls, that is their reusability. Any control should have the ability to be dropped on any page and just work.
Your user and custom controls should be components, that is, they should be independant, encapsulated and reusable entities. It's your page (or containing controls) only that should orchestrate the controls and glue them together. The glue should stay outside and should never ooze inside.
A consequence of that is that your Page generally has no good reason to expose new public properties, because no one should have to consume them.

Comments

haacked@yahoo.com (haacked) said:

I'd make one counter argument to this, though I agree with you for the most part. I wouldn't be opposed to a web framework in which controls could access certain properties of the page.

Bat only through a known interface and if the control degrades gracefully if the Page class didn't support the interface.

For example (inside the control):

IFrameworkPage specialPage = Page as IFrameworkPage;
if(specialPage != null)
//... Access properties of the page specific to this framework
else
//... degrade gracefully
# November 17, 2004 11:11 PM

mike said:

One reason to expose properties on the page is if you want to access them after a Server.Transfer, or in Whidbey, after a cross-post. However, I forget whether those have to be public. Also, I suppose, a reason that people do that is to get access to control properties that are children of the page.
# November 18, 2004 1:12 AM

TrackBack said:

# November 18, 2004 3:02 AM

TrackBack said:

# November 18, 2004 3:03 AM

Bertrand Le Roy said:

These are good points.
Mike's point is especially true, though I personnally don't like cross-page posting: even though we made great improvements in ASP.NET 2.0 in this area, I still think that when you go to a different page, there shouldn't be a strong coupling between both pages. And with the current mechanism, the target page relies on the source page inheriting from a special class or containing special controls. It's strongly coupled. I personally dislike this architecture and prefer to use weaker coupling such as querystring parameters when it's possible.
Mike's second point, though, is precisely what I'm arguing against, if I understood correctly.
# November 18, 2004 1:49 PM

mike said:

You understand correctly -- my point was only that I believe that people sometimes use public page properties to expose control values. (Such an example appears in the docs, I see.) I just checked, and it seems you can use FindControl on the page B to get values out of A -- it still (kind of) breaks encapsulation, since it requires the target page to know the ID of the control to get, but at least it doesn't also require the container page (page A) to add a property to expose the control value(s).
# November 18, 2004 5:54 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)