R – Accessing user controls’s constituent controls from a hosting web page

asp.netuser-controls

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

Best Answer

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.

Related Topic