Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Iterating through Controls Question

Reply
Thread Tools

Iterating through Controls Question

 
 
Andrew
Guest
Posts: n/a
 
      09-28-2004
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


 
Reply With Quote
 
 
 
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      09-28-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Andrew
Guest
Posts: n/a
 
      09-29-2004
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#(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      09-29-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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#(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterating a std::vector vs iterating a std::map? carl C++ 5 11-25-2009 09:55 AM
Iterating through HTML Controls from codebehind RSH ASP .Net 1 09-12-2006 01:16 AM
Iterating through controls in .Net 2.0 Dave H ASP .Net 5 06-30-2006 01:06 AM
problem iterating through controls on a web form. Milsnips ASP .Net 4 10-30-2004 06:50 PM
Iterating through controls (VB) Jeremy ASP .Net 1 07-31-2003 04:27 PM



Advertisments