Loop through every control

S

shapper

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
 
L

Laurent Bugnion [MVP]

Hi,
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
 
E

Eliyahu Goldin

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 said:
Hi,
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
 
L

Laurent Bugnion [MVP]

Hi,

Eliyahu said:
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
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top