Hide all my web user controls

P

Paul Turley

There doesn't appear to be a collection of UserControls on a web form. How
can I iterate through all of my web user controls and set each to
Visible=False?
 
A

Alessandro Zifiglio

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 ;)
 
V

vMike

you can insert a div tag with a runat=server and then make set the visible
property to false. that will remove all the controls between the div tags.
 
A

Alessandro Zifiglio

why un-necessarily use div tags when you can easily iterate through your
pages controls collection and not only set its visible to false but also
work with your controls properties ;P
 
V

vMike

Seems to me if you want to hide all the controls on a page one simple div
tag makes more sense to me then iterating through all the pages controls. Am
I missing something here?
 
A

Alessandro Zifiglio

If you will take the time to read the question you will notice that Paul
specifically asked how he'd go about iterating through his controls
collection.
I really dont see how your div can *simply* resolve.
 
V

vMike

I was simply responding to the subject line "Hide all my web user controls".
His thought was he needed to iterate through the controls. I was just
providing an alternative. I understand your point and your method will do
the same job and provide further alternatives. My method will simply hide
all the web user controls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top