Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Controls.Count, Controls.IsSynchronized, and Controls.SyncRoot

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nathan Sokalski - 04 Sep 2007 23:20 GMT
I have a section of my code in which I need to know how many child controls
a control has. Right now Controls.Count is returning the wrong value, which
I realize is because Controls.IsSynchronized is False. After spending a few
hours trying to figure out how I could determine the number of child
controls, I discovered that Controls.SyncRoot is somehow involved, but I had
trouble understanding exactly what I need to do. Could somebody please help
me? Thanks.
Signature

Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

bruce barker - 04 Sep 2007 23:42 GMT
you are on the wrong track. unless you are spinning up new threads
during a page request and those threads are adding child controls, there
is no need to use Controls.IsSynchronized.

Controls.Count is only your controls immediate count. each of its child
controls can have children. also when you test it, all the children may
not be created.

what makes you think the count is wrong?

-- bruce (sqlwork.com)

> I have a section of my code in which I need to know how many child controls
> a control has. Right now Controls.Count is returning the wrong value, which
[quoted text clipped - 3 lines]
> trouble understanding exactly what I need to do. Could somebody please help
> me? Thanks.
Nathan Sokalski - 05 Sep 2007 03:56 GMT
I know the count is wrong, because first of all, I used the Controls.Add()
method to add multiple Controls, so I know that there are more than 1. Also,
I did a Debug and looked at the Controls.Count property immediately before
and immediately after adding the control, and it had changed from 1 to 2.
And yet, on the next postback that same Control had a Controls.Count value
of 1 again instead of remaining at 2. This did not make since to me, so the
only possibility I could come up with (although I admit I have very little
experience with threads and synchronization, even though this is not a
multi-threaded application) was that it had something to do with all the
synchronization stuff I found while trying to find an answer. I'm totally
confused about this now. Any ideas?
Signature

Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

> you are on the wrong track. unless you are spinning up new threads during
> a page request and those threads are adding child controls, there is no
[quoted text clipped - 15 lines]
>> involved, but I had trouble understanding exactly what I need to do.
>> Could somebody please help me? Thanks.
Muhammad Naveed Yaseen - 05 Sep 2007 04:27 GMT
> And yet, on the next postback that same Control had a Controls.Count value
> of 1 again instead of remaining at 2.

Dynamically added controls do not survive post back. They need to be
added again in every postback.
Muhammad Naveed Yaseen - 05 Sep 2007 01:20 GMT
> I have a section of my code in which I need to know how many child controls
> a control has. Right now Controls.Count is returning the wrong value, which

Just a couple of points to consider,,,,

1) Controls.Count is the count of only immediate children, not the
grand-children.

2) Plain text (sometimes even a space), which apparently may not look
like control, become controls. To add complexity, it is not guaranteed
how many controls would this text divide into.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.