hi paul,
Ok, very interesting question and i just ran some tests
So, heres the deal. You are going to have to iterate through your pages
controls collection. However this can be different when wanting to iteratre
through controls created dynamically or controls you drag and drop on your
webform.
For controls that you drag and drop they are not direct children of your
pages but children of your pages HtmlForm object controls collection. If you
recall, Webcontrols have one important prerequisite and that they be added
in btw <form> tags with runat="server", this surely you already know.
However if your adding controls dynamically this is not true...
so for controls that are not added dynamically first iterate through your
pages controls collection, then look for your usercontrol in your pages
HtmlForm object control collection
To demonstrate this here is an example :
Dim childc As Control
For Each c In Page.Controls
For Each childc In c.Controls
If TypeOf childc Is WebUserControl1 Then
CType(childc, WebUserControl1).Visible = False
End If
Next
Next
*Note in the above example how our usercontrol is a child control of our
HtmlForm object and not a direct child of our page ;P
For dynamically loaded controls instead, just iterating through your pages
controls collection will suffice ;P
Dim c As Control
For Each c In Page.Controls
If TypeOf c Is WebUserControl1 Then
CType(c, WebUserControl1).Visible = False
End If
Next
*Note in the above example how our user control is a direct child of our
page, and not the HtmlForm object controls collection
Also note how in both cases I cType the usercontrol before accessing its
properties this is because c and childc is declared as a control which
returns a type of System.Web.UI.Control, it must be cast to the appropriate
strong type in order to set individual properties of the control. This can
be useless if all you needed was to set the controls visible property coz
all controls have this property, however to access properties specific to
your Usercontrol then this is the way to go
