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

R

Ralf Müller

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
 
J

John Saunders

Ralf Müller said:
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
 
R

Ralf Müller

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

John Saunders said:
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
 
J

John Saunders

Ralf Müller said:
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top