views:

89

answers:

2

Hi


I’ve read that user control’s constituent controls can be accessed only by user control and thus web page that hosts this user control cannot receive the events, call methods or set properties of these contained controls.

But I’m not sure the above claim is true, since I was able to access ( from hosting web page ) ClickButton.Click event ( assume WebUserControl1 contains ClickButton control ):

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         Button ClickButton = (Button)WebUserControl1.Controls[0];
         ClickButton.Click += someClickHandler;
    }


thanx

+1  A: 

You can expose the user control's properties (i.e. settings), controls, and events publicly which means you don't have to find the control within the usercontrol.

Robert W
I realize that, but what I was trying to imply is, that hosting web page should only have access to those details ( events, methods and properties ) of contained controls ( by contained controls I'm reffering to controls inside user control ), which user control decides to expose. But due to WebUserControl1.Controls dictionary, all the details ( events, methods and properties ) of contained control are exposed, whether you like it or not.For example, perhaps I want contained control's method to only be accesible to hosting user control, but
carewithl
due to WebUserControl.Controls dictionary, this event will also be accessible to a web site hosting this user control
carewithl
+1  A: 

The entire page is a tree of controls. You can "browse" through this tree regardless of the parent of that control. For example from inside a user control you could go down to the parent, which could be another control, then further down to the page, then to the master page, and so on.

So yes, you are correct, it's not hidden to the point you can't access it, but it's not published either. In a similar way using reflection you could call private methods that you couldn't otherwise. Using certain tools you could access and change code that's already compiled; so nothing is really out of reach.

These boundaries are set and used to minimize complexity, not as an absolute wall that cannot be crossed.

pbz
thank you for helping me out
carewithl