Programmatically manipulating all controls of type X?

K

Ken Fine

I'm interested in programmatically manipulating groups of ASP.NET controls
by type. Can someone suggest code for the following?

Loop through, say, all label controls on a page, and assigning a CssClass to
them, or programmatically making the Visible/not Visible.

If applied to a containing page, such a function would traverse all user
controls statically and dynamically placed on the page, correct?

Thanks,
-KF
 
E

Eliyahu Goldin

Although you can loop recursively through all controls on the page starting
from this.Controls collection, check the type of every control and take
actions for required types, it doesn't seem to be an elegant solution. Why
don't you manipulate css rules instead? A css for span will cover all
labels. If you change it programmatically, all labels will change their
appearance.
 
K

Ken Fine

Thank you, Eliyahu, I thought about CSS as a solution for the specific case
I cited. My question more generally is about ways to programmatically
investigate the page, and I'd still like to know all of the ways I might
manage this.

I thought of another case that would be useful in addition to my earlier
query. So what I'd still like to know at this point is:

* how can I loop recursively through all controls on the page and manipulate
controls of type X
* how could I loop recursively through all controls on a page and manipulate
user controls with custom properties set to a specific value? What is the
last point in the page lifecycle that I would still be "permitted" to run
such a function (i.e. could some user control values be set
programmatically, and my looping function be run prior to the page being
rendered?)

-KF


Eliyahu Goldin said:
Although you can loop recursively through all controls on the page
starting from this.Controls collection, check the type of every control
and take actions for required types, it doesn't seem to be an elegant
solution. Why don't you manipulate css rules instead? A css for span will
cover all labels. If you change it programmatically, all labels will
change their appearance.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Ken Fine said:
I'm interested in programmatically manipulating groups of ASP.NET
controls by type. Can someone suggest code for the following?

Loop through, say, all label controls on a page, and assigning a CssClass
to them, or programmatically making the Visible/not Visible.

If applied to a containing page, such a function would traverse all user
controls statically and dynamically placed on the page, correct?

Thanks,
-KF
 
E

Eliyahu Goldin

Something like this (I neither run nor compiled this code):

private void checkChildrenControls (Control control)
{
if (control.Controls != null)
foreach (Control childControl in control.Controls)
{
checkControl (childControl);
checkChildrenControls (childControl);
}
}

private void checkControl (Control control)
{
if (control is Label)
{
Label labelControl = Control as Label;
....
}
}

and somewhere in the Page methods:

this.checkChildrenControls (this);
..

You last point is the page's PreRender event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Ken Fine said:
Thank you, Eliyahu, I thought about CSS as a solution for the specific case
I cited. My question more generally is about ways to programmatically
investigate the page, and I'd still like to know all of the ways I might
manage this.

I thought of another case that would be useful in addition to my earlier
query. So what I'd still like to know at this point is:

* how can I loop recursively through all controls on the page and manipulate
controls of type X
* how could I loop recursively through all controls on a page and manipulate
user controls with custom properties set to a specific value? What is the
last point in the page lifecycle that I would still be "permitted" to run
such a function (i.e. could some user control values be set
programmatically, and my looping function be run prior to the page being
rendered?)

-KF


Eliyahu Goldin said:
Although you can loop recursively through all controls on the page
starting from this.Controls collection, check the type of every control
and take actions for required types, it doesn't seem to be an elegant
solution. Why don't you manipulate css rules instead? A css for span will
cover all labels. If you change it programmatically, all labels will
change their appearance.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Ken Fine said:
I'm interested in programmatically manipulating groups of ASP.NET
controls by type. Can someone suggest code for the following?

Loop through, say, all label controls on a page, and assigning a CssClass
to them, or programmatically making the Visible/not Visible.

If applied to a containing page, such a function would traverse all user
controls statically and dynamically placed on the page, correct?

Thanks,
-KF
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top