Page.Controls - controls are missing?

  • Thread starter Thread starter Frank Esser
  • Start date Start date
F

Frank Esser

Hi,

I created an ASP.NET page (test.aspx) with some web controls in GridLayout
(Design time).

When I look at the collection page.controls by

foreach (Control ctrl in Page.Controls)
{
....
}

then I can not see all the controls on the page.

Is there a special controls collection for the design time gridlayout
controls?

Best regards
Frank
(newbie)
 
Hallo Eliyahu ,

thanks for your answer.

How can I iterate through the controls that were created at design time?
For example when I want to make all available controls invisible or disabled
at runtime?
At the moment I have to call them directly by name. This is very ugly for my
coding...

Thanks
Frank
 
I must confess that in similar situations I do operate on controls
individually. Just to add a few extra lines of code is often a better idea
than spending hours on looking for more elegant solutions. If you don't want
this, you can either navigate Controls collections or try find a sort of
container element (a panel?) that would disable its children when it is
disabled.

Eliyahu
 
But if I want to write a global function that makes all controls on a web
page invisible for example then I have to use a generic way of accessing the
page controls...

I can not imagine that there is no way to do that in the .NET framework or
ASP.NET.
That would really be a strange mistake by Microsoft...

Other opinions?
 
Hi Frank,

you can iterate through all controls in your page after the Init-Event was
executed. In case you want to set all sub-control unvisible do the following:

public class MyControl : System.Web.UI.Control /* or any other base-class
e.g. System.Web.UI.WebControls.WebControl */

...

void setVisibility(bool visible)
{
foreach (Control ctl in this.Controls)
{
ctl.Visible = visible;
}
}

...

}

Hope that helps
patrick
 
This will affect only direct children. You need to implement recursion to go
all the way down.

Eliyahu
 
That's nice. I've got thanks for an article written by Steve.

That's an ideal example how newsgroup should work.

Question > Directions > Research > Answer.

Eliyahu
 
Hi Eliyahu,

in case of visibility you do NOT have to implement recursion. When the
parent is not visible childcontrols are displayed neither.

Or am I wrong?
patrick
 

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

Back
Top