By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,018 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,018 IT Pros & Developers. It's quick & easy.

Programmatically manipulating all controls of type X?

P: n/a
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
Dec 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <ke*****@u.washington.eduwrote in message
news:em**********@gnus01.u.washington.edu...
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

Dec 26 '06 #2

P: n/a
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" <RE**************************@mMvVpPsS.orgwrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
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" <ke*****@u.washington.eduwrote in message
news:em**********@gnus01.u.washington.edu...
>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


Dec 26 '06 #3

P: n/a
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" <ke*****@u.washington.eduwrote in message
news:em**********@gnus01.u.washington.edu...
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" <RE**************************@mMvVpPsS.orgwrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
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" <ke*****@u.washington.eduwrote in message
news:em**********@gnus01.u.washington.edu...
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


Dec 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.