Iterating through Controls Question

Discussion in 'ASP .Net Web Controls' started by Andrew, Sep 28, 2004.

  1. Andrew

    Andrew Guest

    Hey all,

    I am still getting the hang of .Net so I'm posting this question looking
    for someone to maybe explain the following process to me so I can better
    understand just what is going on here.
    I have an aspx page with a few Panel Web Controls on it. I turn them on
    and off depending on what the user is doing. I wanted to make the code a
    little shorter on the back end by writing a subroutine that turns off all
    panels by iterating through the controls collection:

    My first attempt didn't go so well:
    For Each p As Panel In Me.Controls
    p.Visible = False
    Next

    After some searching on Google, CodeProject, and the MSDN help files, I
    was able to correct the function so it worked:
    For Each c As Control In Me.Controls.Item(1).Controls
    If TypeOf c Is Panel Then
    c.Visible = False
    End If
    Next

    My question is in two parts: 1 - Obviously, why did the first one not
    work, and 2 - Why do I need to use "Me.Controls.Item(1).Controls" to access
    the controls collection for the page?

    Again, I am still getting a handle on all this, so I appreciate any
    replies to be as simple and straight forward as possible. :) Thanks!

    -- Andrew
     
    Andrew, Sep 28, 2004
    #1
    1. Advertising

  2. i could explain but the easier way to understand it is to step thru loop 1
    and 2 with the debugger
    paying attention to what is returned. then you can use the watch window to
    see how the control
    container is setup. that way you will understand what is going on internally
    as opposed to reading
    fluffy stuff. its the best way to learn.

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Andrew" <> wrote in message
    news:...
    > Hey all,
    >
    > I am still getting the hang of .Net so I'm posting this question
    > looking
    > for someone to maybe explain the following process to me so I can better
    > understand just what is going on here.
    > I have an aspx page with a few Panel Web Controls on it. I turn them
    > on
    > and off depending on what the user is doing. I wanted to make the code a
    > little shorter on the back end by writing a subroutine that turns off all
    > panels by iterating through the controls collection:
    >
    > My first attempt didn't go so well:
    > For Each p As Panel In Me.Controls
    > p.Visible = False
    > Next
    >
    > After some searching on Google, CodeProject, and the MSDN help files, I
    > was able to correct the function so it worked:
    > For Each c As Control In Me.Controls.Item(1).Controls
    > If TypeOf c Is Panel Then
    > c.Visible = False
    > End If
    > Next
    >
    > My question is in two parts: 1 - Obviously, why did the first one not
    > work, and 2 - Why do I need to use "Me.Controls.Item(1).Controls" to
    > access
    > the controls collection for the page?
    >
    > Again, I am still getting a handle on all this, so I appreciate any
    > replies to be as simple and straight forward as possible. :) Thanks!
    >
    > -- Andrew
    >
    >
     
    Alvin Bruney [MVP], Sep 29, 2004
    #2
    1. Advertising

  3. Andrew

    Andrew Guest

    Alvin,

    I appreciate the time and effort of your response and thank you, but I have
    the thing working just fine and using the debugger was part of how I managed
    that. With this application, I have the "what", I am attempting to
    understand the "why". Not harping on you, but sometimes, I really am after
    the answer to the question I asked. I just have to leave it up to those out
    here to read the question as I wrote it.

    -- Andrew

    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:#t#...
    > i could explain but the easier way to understand it is to step thru loop 1
    > and 2 with the debugger
    > paying attention to what is returned. then you can use the watch window to
    > see how the control
    > container is setup. that way you will understand what is going on

    internally
    > as opposed to reading
    > fluffy stuff. its the best way to learn.
    >
    > --
    > Regards,
    > Alvin Bruney
    > [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    > Got tidbits? Get it here... http://tinyurl.com/27cok
    > "Andrew" <> wrote in message
    > news:...
    > > Hey all,
    > >
    > > I am still getting the hang of .Net so I'm posting this question
    > > looking
    > > for someone to maybe explain the following process to me so I can better
    > > understand just what is going on here.
    > > I have an aspx page with a few Panel Web Controls on it. I turn them
    > > on
    > > and off depending on what the user is doing. I wanted to make the code

    a
    > > little shorter on the back end by writing a subroutine that turns off

    all
    > > panels by iterating through the controls collection:
    > >
    > > My first attempt didn't go so well:
    > > For Each p As Panel In Me.Controls
    > > p.Visible = False
    > > Next
    > >
    > > After some searching on Google, CodeProject, and the MSDN help files,

    I
    > > was able to correct the function so it worked:
    > > For Each c As Control In Me.Controls.Item(1).Controls
    > > If TypeOf c Is Panel Then
    > > c.Visible = False
    > > End If
    > > Next
    > >
    > > My question is in two parts: 1 - Obviously, why did the first one not
    > > work, and 2 - Why do I need to use "Me.Controls.Item(1).Controls" to
    > > access
    > > the controls collection for the page?
    > >
    > > Again, I am still getting a handle on all this, so I appreciate any
    > > replies to be as simple and straight forward as possible. :) Thanks!
    > >
    > > -- Andrew
    > >
    > >

    >
    >
     
    Andrew, Sep 29, 2004
    #3
  4. roughly
    a panel is unique in its ability to house other controls in a collection.
    when you iterated the panel in the first loop, you returned references to
    the controls
    collections. your second loop probes the collection of controls and returns
    the first control from the collection which is off type panel. loop 1 can be
    made to work
    by refining the returned results by the way.

    hth


    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Andrew" <> wrote in message
    news:...
    > Alvin,
    >
    > I appreciate the time and effort of your response and thank you, but I
    > have
    > the thing working just fine and using the debugger was part of how I
    > managed
    > that. With this application, I have the "what", I am attempting to
    > understand the "why". Not harping on you, but sometimes, I really am
    > after
    > the answer to the question I asked. I just have to leave it up to those
    > out
    > here to read the question as I wrote it.
    >
    > -- Andrew
    >
    > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > news:#t#...
    >> i could explain but the easier way to understand it is to step thru loop
    >> 1
    >> and 2 with the debugger
    >> paying attention to what is returned. then you can use the watch window
    >> to
    >> see how the control
    >> container is setup. that way you will understand what is going on

    > internally
    >> as opposed to reading
    >> fluffy stuff. its the best way to learn.
    >>
    >> --
    >> Regards,
    >> Alvin Bruney
    >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    >> Got tidbits? Get it here... http://tinyurl.com/27cok
    >> "Andrew" <> wrote in message
    >> news:...
    >> > Hey all,
    >> >
    >> > I am still getting the hang of .Net so I'm posting this question
    >> > looking
    >> > for someone to maybe explain the following process to me so I can
    >> > better
    >> > understand just what is going on here.
    >> > I have an aspx page with a few Panel Web Controls on it. I turn
    >> > them
    >> > on
    >> > and off depending on what the user is doing. I wanted to make the code

    > a
    >> > little shorter on the back end by writing a subroutine that turns off

    > all
    >> > panels by iterating through the controls collection:
    >> >
    >> > My first attempt didn't go so well:
    >> > For Each p As Panel In Me.Controls
    >> > p.Visible = False
    >> > Next
    >> >
    >> > After some searching on Google, CodeProject, and the MSDN help
    >> > files,

    > I
    >> > was able to correct the function so it worked:
    >> > For Each c As Control In Me.Controls.Item(1).Controls
    >> > If TypeOf c Is Panel Then
    >> > c.Visible = False
    >> > End If
    >> > Next
    >> >
    >> > My question is in two parts: 1 - Obviously, why did the first one
    >> > not
    >> > work, and 2 - Why do I need to use "Me.Controls.Item(1).Controls" to
    >> > access
    >> > the controls collection for the page?
    >> >
    >> > Again, I am still getting a handle on all this, so I appreciate any
    >> > replies to be as simple and straight forward as possible. :) Thanks!
    >> >
    >> > -- Andrew
    >> >
    >> >

    >>
    >>

    >
    >
     
    Alvin Bruney [MVP], Sep 29, 2004
    #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. Jeremy

    Iterating through controls (VB)

    Jeremy, Jul 31, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,485
    Marina
    Jul 31, 2003
  2. Milsnips
    Replies:
    4
    Views:
    2,353
    Scott M.
    Oct 30, 2004
  3. Dave H
    Replies:
    5
    Views:
    543
    Walter Wang [MSFT]
    Jun 30, 2006
  4. RSH
    Replies:
    1
    Views:
    419
    Rob MacFadyen
    Sep 12, 2006
  5. carl
    Replies:
    5
    Views:
    2,427
    James Kanze
    Nov 25, 2009
Loading...

Share This Page