Iterating through Controls Question

A

Andrew

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
 
A

Alvin Bruney [MVP]

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.
 
A

Andrew

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

Alvin Bruney [MVP]

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

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top