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

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

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

    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 Guest

  2. Similar Questions and Discussions

    1. Question about Controls collection, CreateChildControls and Render methods
      Hi, Consider this code (simplified) example: public class Person : Microsoft.SharePoint.WebPartPages.WebPart { private ImageButton _action =...
    2. custom control (with collection) do not render at Design time
      Hello, I built a custom control... Everithing is working fine... EXCEPT... ;-) When I first place the control on the webform at design time it...
    3. System.InvalidOperationException: ExecuteReader: Connection property has not been initialized
      Hello, I dont understand whats going on with my DB2 connector. I used to have the code in a class that worked fine. But when I tried to use it in a...
    4. System.InvalidOperationException: The type myobject[] may not be used in this context.
      I am trying to return a custom object from a web service, and get this very cryptic message. System.InvalidOperationException: There was an error...
    5. Adding a control to the DataGrid collection controls
      Hi I have built a DataGrid custom adding a panel of selection automatically produced in base to the columns of the DataGrid The panel has...
  3. #2

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

    "Ralf Müller" <mueller@julitec.de> wrote in message
    news:eRaCJMS3EHA.3120@TK2MSFTNGP12.phx.gbl...
    > 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 Guest

  4. #3

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

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

    "John Saunders" <johnwsaundersiii at hotmail.com> schrieb im Newsbeitrag
    news:e8YwYsV3EHA.3132@TK2MSFTNGP14.phx.gbl...
    > "Ralf Müller" <mueller@julitec.de> wrote in message
    > news:eRaCJMS3EHA.3120@TK2MSFTNGP12.phx.gbl...
    > > 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 Guest

  5. #4

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

    "Ralf Müller" <mueller@julitec.de> wrote in message
    news:u$5E$ud3EHA.2676@TK2MSFTNGP12.phx.gbl...
    > 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:e8YwYsV3EHA.3132@TK2MSFTNGP14.phx.gbl...
    >> "Ralf Müller" <mueller@julitec.de> wrote in message
    >> news:eRaCJMS3EHA.3120@TK2MSFTNGP12.phx.gbl...
    >> > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139