Saturday, March 24, 2012

Loop through every control

Hello,

I have a panel which contains various textboxes and dropdownlists.

I want to loop through each control inside the panel and detected if it
is a DropDownList or a TextBox so I can access its SelectedValue or
Text.

How can I do this?

Thanks,

MiguelHi,

shapper wrote:

Quote:

Originally Posted by

Hello,
>
I have a panel which contains various textboxes and dropdownlists.
>
I want to loop through each control inside the panel and detected if it
is a DropDownList or a TextBox so I can access its SelectedValue or
Text.
>
How can I do this?
>
Thanks,
>
Miguel


foreach ( Control child in yourPanel.Controls )
{
if ( child is DropDownList )
{
( (DropDownList) child ).SelectedValue = "...";
}
if ( child is TextBox )
{
( (TextBox) child ).Text = "...";
}
}

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
This is good only if the controls are directly in the panel. Otherwise a
recursion is needed.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Laurent Bugnion [MVP]" <galasoft-lb@.bluewin.chwrote in message
news:%23NoAEFYNHHA.5064@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

Hi,
>
shapper wrote:

Quote:

Originally Posted by

>Hello,
>>
>I have a panel which contains various textboxes and dropdownlists.
>>
>I want to loop through each control inside the panel and detected if it
>is a DropDownList or a TextBox so I can access its SelectedValue or
>Text.
>>
>How can I do this?
>>
>Thanks,
>>
>Miguel


>
foreach ( Control child in yourPanel.Controls )
{
if ( child is DropDownList )
{
( (DropDownList) child ).SelectedValue = "...";
}
if ( child is TextBox )
{
( (TextBox) child ).Text = "...";
}
}
>
HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch


Hi,

Eliyahu Goldin wrote:

Quote:

Originally Posted by

This is good only if the controls are directly in the panel. Otherwise a
recursion is needed.


Yes, but the OP said "I have a panel which contains various textboxes
and dropdownlists."

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

0 comments:

Post a Comment