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

Discussion in 'ASP .Net' started by Nathan Sokalski, Sep 4, 2007.

  1. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Sep 4, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    bruce barker Guest

    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)


    Nathan Sokalski wrote:
    > 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.
     
    bruce barker, Sep 4, 2007
    #2
    1. Advertising

  3. > 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.
     
    Muhammad Naveed Yaseen, Sep 5, 2007
    #3
  4. 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?
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "bruce barker" <> wrote in message
    news:...
    > 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)
    >
    >
    > Nathan Sokalski wrote:
    >> 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.
     
    Nathan Sokalski, Sep 5, 2007
    #4
  5. > 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, Sep 5, 2007
    #5
    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. Invalidlastname
    Replies:
    7
    Views:
    831
    Steven Cheng[MSFT]
    Jan 16, 2004
  2. Chad Scharf
    Replies:
    3
    Views:
    924
    Chad Scharf
    Oct 18, 2007
  3. efelnavarro09
    Replies:
    2
    Views:
    954
    efelnavarro09
    Jan 26, 2011
  4. Craig Burkett
    Replies:
    2
    Views:
    648
    Craig Burkett
    Sep 11, 2003
  5. hara
    Replies:
    4
    Views:
    164
    David Squire
    May 25, 2006
Loading...

Share This Page