Loop through every control

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top