Note, before marking as duplicate - similar questions have been asked before, but all reference specific third-party libraries with solutions to match. This question relates solely to the native ASP.Net framework, and I am looking for an explanation of why this behaviour occurs in addition to the best solution.
I have a Page structure like this:
Within ControlA, I expose the textbox as a Public Property
so that I can access it at page-level. So, at page-level, this works perfectly:
Trace.Warn(ControlA.TextBoxA.Text) ' prints "Control A Text Box"
However, exposing ControlB's textbox as a public property in the same manner does not allow it to be available at page-level. This code at page-level...
Trace.Warn(ControlA.NestedControlB.TextBoxB.Text)
...fails with this error:
BC30652: Reference required to assembly 'App_Web_3jwc5ppn
The same is also true of any methods that are placed in ControlB that need to be accessible at page-level. Clicking the "Add Reference" link achieves nothing, and moreso, the assembly title changes every time the project is rebuilt.
A solution, but is it the best for complex controls?
I can bubble up the properties by declaring the ControlB textbox as a property in ControlA, like this:
Public ReadOnly Property NestedControlBTextBox As TextBox
Get
Return ControlB.TextBoxB()
End Get
End Property
This allows the nested textbox to be accessible at page-level with this:
Trace.Warn(ControlA.NestedControlBTextBox.Text) ' prints "Control B Text Box"
But is this the best/correct approach? It seems like an awful lot of work to bubble up every property and/or event up N levels of hierarchy. Obviously my real ASCX controls are way more complex than this.
I have enclosed the solution in case anyone wants to illustrate a better approach.