Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Loop through every control (http://www.velocityreviews.com/forums/t385044-loop-through-every-control.html)

shapper 01-11-2007 12:48 PM

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,

Miguel


Laurent Bugnion [MVP] 01-11-2007 01:06 PM

Re: Loop through every control
 
Hi,

shapper wrote:
> 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

Eliyahu Goldin 01-11-2007 01:33 PM

Re: Loop through every control
 
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.ch> wrote in message
news:%23NoAEFYNHHA.5064@TK2MSFTNGP04.phx.gbl...
> Hi,
>
> shapper wrote:
>> 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




Laurent Bugnion [MVP] 01-11-2007 02:04 PM

Re: Loop through every control
 
Hi,

Eliyahu Goldin wrote:
> 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


All times are GMT. The time now is 12:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.