Loop through every control

Discussion in 'ASP .Net' started by shapper, Jan 11, 2007.

  1. shapper

    shapper Guest

    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
     
    shapper, Jan 11, 2007
    #1
    1. Advertising

  2. 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], Jan 11, 2007
    #2
    1. Advertising

  3. 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]" <> wrote in message
    news:%...
    > 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, Jan 11, 2007
    #3
  4. Laurent Bugnion [MVP], Jan 11, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chaos
    Replies:
    30
    Views:
    1,205
    Chaos
    Aug 6, 2006
  2. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,496
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
  3. Roedy Green
    Replies:
    3
    Views:
    455
    Mike Schilling
    Sep 13, 2008
  4. K Viltersten
    Replies:
    0
    Views:
    381
    K Viltersten
    Aug 22, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    452
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page