Enumerating Controls Collection in Control.Render() causes System.InvalidOperationException - Why???

Discussion in 'ASP .Net Building Controls' started by Ralf Müller, Dec 8, 2004.

  1. Ralf Müller

    Ralf Müller Guest

    Hi all!

    any ideas why this code...

    protected override void Render(HtmlTextWriter writer) {
    Table table;
    TableRow row;
    TableCell cell;

    table = new Table();

    foreach(ListElement e in Controls) {
    row = new TableRow();
    cell = new TableCell();
    cell.Controls.Add(e);
    row.Cells.Add(cell);
    table.Rows.Add(row);
    }

    table.RenderControl(writer);
    }

    .... produces the following Exception in my ListElementCollection (Composite
    Control):

    "System.InvalidOperationException: Die Liste, an die der Enumerator gebunden
    ist, wurde geändert. Ein Enumerator kann nur verwendet werden, wenn die
    Liste nicht geändert wird."
    Sorry - I got the german language version of Visual Studio. It says that the
    List the Enumerator is bound to has been altered and that an Enumerator can
    only be used when the List is not altered. - But WHY ON EARTH is my Controls
    Collection altered while my Control is being rendered???


    Greetings, Ralf
    Ralf Müller, Dec 8, 2004
    #1
    1. Advertising

  2. "Ralf Müller" <> wrote in message
    news:...
    > Hi all!
    >
    > any ideas why this code...
    >
    > protected override void Render(HtmlTextWriter writer) {
    > Table table;
    > TableRow row;
    > TableCell cell;
    >
    > table = new Table();
    >
    > foreach(ListElement e in Controls) {
    > row = new TableRow();
    > cell = new TableCell();
    > cell.Controls.Add(e);
    > row.Cells.Add(cell);
    > table.Rows.Add(row);
    > }
    >
    > table.RenderControl(writer);
    > }
    >
    > ... produces the following Exception in my ListElementCollection
    > (Composite
    > Control):
    >
    > "System.InvalidOperationException: Die Liste, an die der Enumerator
    > gebunden
    > ist, wurde geändert. Ein Enumerator kann nur verwendet werden, wenn die
    > Liste nicht geändert wird."
    > Sorry - I got the german language version of Visual Studio. It says that
    > the
    > List the Enumerator is bound to has been altered and that an Enumerator
    > can
    > only be used when the List is not altered. - But WHY ON EARTH is my
    > Controls
    > Collection altered while my Control is being rendered???


    Aren't you modifying your Controls collection when you add e (a member of
    the Controls collection) to the Controls collection of the cell. A control
    may only be in a single Controls collection.

    In a case like this, I have stored my sub-controls in a collection separate
    from the Controls collection. I then enumerate my private collection for
    rendering.

    John Saunders
    John Saunders, Dec 8, 2004
    #2
    1. Advertising

  3. Ralf Müller

    Ralf Müller Guest

    Thank you John! Wow - that was a dumb mistake!

    "John Saunders" <johnwsaundersiii at hotmail.com> schrieb im Newsbeitrag
    news:...
    > "Ralf Müller" <> wrote in message
    > news:...
    > > Hi all!
    > >
    > > any ideas why this code...
    > >
    > > protected override void Render(HtmlTextWriter writer) {
    > > Table table;
    > > TableRow row;
    > > TableCell cell;
    > >
    > > table = new Table();
    > >
    > > foreach(ListElement e in Controls) {
    > > row = new TableRow();
    > > cell = new TableCell();
    > > cell.Controls.Add(e);
    > > row.Cells.Add(cell);
    > > table.Rows.Add(row);
    > > }
    > >
    > > table.RenderControl(writer);
    > > }
    > >
    > > ... produces the following Exception in my ListElementCollection
    > > (Composite
    > > Control):
    > >
    > > "System.InvalidOperationException: Die Liste, an die der Enumerator
    > > gebunden
    > > ist, wurde geändert. Ein Enumerator kann nur verwendet werden, wenn die
    > > Liste nicht geändert wird."
    > > Sorry - I got the german language version of Visual Studio. It says that
    > > the
    > > List the Enumerator is bound to has been altered and that an Enumerator
    > > can
    > > only be used when the List is not altered. - But WHY ON EARTH is my
    > > Controls
    > > Collection altered while my Control is being rendered???

    >
    > Aren't you modifying your Controls collection when you add e (a member of
    > the Controls collection) to the Controls collection of the cell. A control
    > may only be in a single Controls collection.
    >
    > In a case like this, I have stored my sub-controls in a collection

    separate
    > from the Controls collection. I then enumerate my private collection for
    > rendering.
    >
    > John Saunders
    >
    >
    Ralf Müller, Dec 9, 2004
    #3
  4. "Ralf Müller" <> wrote in message
    news:u$5E$...
    > Thank you John! Wow - that was a dumb mistake!


    Not really dumb. It's just that many .NET collections work this way. A
    DataRow can only be in one DataTable.Rows collection, for instance.

    John Saunders

    > "John Saunders" <johnwsaundersiii at hotmail.com> schrieb im Newsbeitrag
    > news:...
    >> "Ralf Müller" <> wrote in message
    >> news:...
    >> > Hi all!
    >> >
    >> > any ideas why this code...
    >> >
    >> > protected override void Render(HtmlTextWriter writer) {
    >> > Table table;
    >> > TableRow row;
    >> > TableCell cell;
    >> >
    >> > table = new Table();
    >> >
    >> > foreach(ListElement e in Controls) {
    >> > row = new TableRow();
    >> > cell = new TableCell();
    >> > cell.Controls.Add(e);
    >> > row.Cells.Add(cell);
    >> > table.Rows.Add(row);
    >> > }
    >> >
    >> > table.RenderControl(writer);
    >> > }
    >> >
    >> > ... produces the following Exception in my ListElementCollection
    >> > (Composite
    >> > Control):
    >> >
    >> > "System.InvalidOperationException: Die Liste, an die der Enumerator
    >> > gebunden
    >> > ist, wurde geändert. Ein Enumerator kann nur verwendet werden, wenn die
    >> > Liste nicht geändert wird."
    >> > Sorry - I got the german language version of Visual Studio. It says
    >> > that
    >> > the
    >> > List the Enumerator is bound to has been altered and that an Enumerator
    >> > can
    >> > only be used when the List is not altered. - But WHY ON EARTH is my
    >> > Controls
    >> > Collection altered while my Control is being rendered???

    >>
    >> Aren't you modifying your Controls collection when you add e (a member of
    >> the Controls collection) to the Controls collection of the cell. A
    >> control
    >> may only be in a single Controls collection.
    >>
    >> In a case like this, I have stored my sub-controls in a collection

    > separate
    >> from the Controls collection. I then enumerate my private collection for
    >> rendering.
    >>
    >> John Saunders
    >>
    >>

    >
    >
    John Saunders, Dec 9, 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. Jeff Kennedy
    Replies:
    0
    Views:
    1,515
    Jeff Kennedy
    Oct 20, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,764
    Smokey Grindel
    Dec 2, 2006
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    949
    Øyvind Isaksen
    May 18, 2007
  4. DC
    Replies:
    3
    Views:
    410
  5. Mark
    Replies:
    0
    Views:
    92
Loading...

Share This Page