My Auto Incrementing NumberedLabel doesn't work after upgrading to ASP.NET 2.0

A

Atif Sarfraz

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=========================================================

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
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top