Collection was modified; enumeration operation may

Discussion in 'ASP .Net Building Controls' started by Atif Sarfraz, Dec 1, 2006.

  1. Atif Sarfraz

    Atif Sarfraz Guest

    Hi,

    I had written a custom WebControl called NumberedLabel (Inherited from
    Label) to provide the Auto Numbered bullet functionality (as in Word). This
    used to work fine before I migrated my web project to .NET 2.0. All you
    needed to do was to drop these Numbered labels on the form, and they would
    auto increment (even hiding intermediate labels doesn't bother with the
    numbering).

    However since I ported my website to ASP.NET 2.0 I get an error on my page
    where I am using the controls. Remember that my control class library wasn't
    modified much in the porting to .NET 2.0 as I don't see any change in the
    code.
    The error message is ==> Collection was modified; enumeration operation may
    not execute.

    The controls still continue to work well, and do generate an auto-numbered
    label, however after all the page has loaded the above message is thrown in
    the output of my page below.

    Here is a complete code of the control and the error is somehow linked to
    the following line Me.Page.Controls.Add(counterControl) in the function
    IncrementCount.

    Any help would be greatly appreciated.

    Regards,

    Atif Sarfraz

    ==========================================NumberedLabel
    Code========================================================

    Imports System.ComponentModel
    Imports System.Web.UI

    <DefaultProperty("Text"), ToolboxData("<{0}:NumberedLabel
    runat=server></{0}:NumberedLabel>")> Public Class NumberedLabel
    Inherits System.Web.UI.WebControls.Label

    Dim _text As String
    Dim _groupName As String = ""
    Dim _format As String = ""
    Private Const DefaultGroupName = "GroupName1" ' This is the default
    groupname to be used
    Private Const DefaultFormat = "{0}." ' This is the default
    format to be used

    <Bindable(True), Category("Appearance"), DefaultValue("")> Overrides
    Property [Text]() As String
    Get
    Return _text
    End Get

    Set(ByVal Value As String)
    _text = Value
    End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    [GroupName]() As String
    Get
    If _groupName.Trim.Equals("") Then
    Return DefaultGroupName
    Else
    Return _groupName
    End If
    End Get

    Set(ByVal Value As String)
    _groupName = Value
    End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    [DisplayFormat]() As String
    Get
    ' This is just done to show the default format
    If _format.Trim.Equals("") Then
    Return DefaultFormat
    Else
    Return _format
    End If
    End Get

    Set(ByVal Value As String)
    _format = Value
    End Set
    End Property

    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)
    Try

    If _groupName.Trim.Equals("") Then
    IncrementCount(DefaultGroupName)
    Else
    IncrementCount(_groupName)
    End If

    ' Format and display
    If _format.Trim.Equals("") Then
    _text = String.Format(DefaultFormat, [Text])
    Else
    _text = String.Format(_format, [Text])
    End If

    ' Ask the base to render the output
    MyBase.Render(output)

    Catch ex As Exception
    Throw
    End Try

    End Sub

    Private Sub IncrementCount(ByVal Name As String)

    Dim count As Integer = 1
    Dim counterControl As LiteralControl


    If Me.Page.FindControl(Name) Is Nothing Then
    counterControl = New LiteralControl
    counterControl.ID = Name
    counterControl.Text = count
    counterControl.Visible = False
    Me.Page.Controls.Add(counterControl)
    Else
    Try
    counterControl = CType(Me.Page.FindControl(Name),
    LiteralControl)
    count = CInt(counterControl.Text)
    count += 1
    counterControl.Text = count
    Catch ex As Exception
    ' Shouldn't happen, just to make sure that we don't get
    another control of the same name as ours
    End Try
    End If

    _text = count

    End Sub

    End Class
    Atif Sarfraz, Dec 1, 2006
    #1
    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. Brian Riis
    Replies:
    0
    Views:
    464
    Brian Riis
    Oct 21, 2004
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    959
    Øyvind Isaksen
    May 18, 2007
  3. puvit82
    Replies:
    4
    Views:
    757
    puvit82
    Feb 1, 2008
  4. Helmut Jarausch
    Replies:
    20
    Views:
    2,104
    Johannes Schaub (litb)
    Oct 22, 2009
  5. Jon

    Databinding to a collection with a enumeration property

    Jon, Jul 13, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    122
    Eliyahu Goldin
    Jul 14, 2004
Loading...

Share This Page