Problem with two instances of a custom class

A

anr

Hi all,

I have a problem with my XCalendar custom class. If I put
in the page one instance it works, but if i put two or more
throw an error like this:

"Multiple controls with the same ID".

The class is contained basically by a Textbox and
CalendarExtender control. And the error is is the
hightlighted line.

VB CLASS
Code:
Imports Microsoft.VisualBasic
Imports AjaxControlToolkit

Public Class XCalendar
    Private _date As DateTime
    Private _calendar As CalendarExtender
    Private _textbox As TextBox
    Private _defaulttext As String
    Private _backcolor As Drawing.Color
    Private _forecolor As Drawing.Color
    Private _image As Image
    Private _onclientdateselectionchanged As String
    Private _random As New Random
    Private _randomnum As String = Nothing

    ''' <summary>
    ''' (By Default "(Select)")
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property DefaultText() As String
        Get
            Return _defaulttext
        End Get
        Set(ByVal value As String)
            _defaulttext = value
        End Set
    End Property

    ''' <summary>
    ''' Control BackColor (By Default White)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property BackColor() As Drawing.Color
        Get
            Return _backcolor
        End Get
        Set(ByVal value As Drawing.Color)
            _backcolor = value
        End Set
    End Property

    ''' <summary>
    ''' (By Default Black)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ForeColor() As Drawing.Color
        Get
            Return _forecolor
        End Get
        Set(ByVal value As Drawing.Color)
            _forecolor = value
        End Set
    End Property

    ''' <summary>
    ''' Returns Selected Date
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ReturnDate() As Date
        Get
            Return _date
        End Get
    End Property

    ''' <summary>
    ''' Shows the Control XCalendar
    ''' </summary>
    ''' <param name="XDate">Date (Optional)</param>
    ''' <returns>Table with the control</returns>
    ''' <remarks></remarks>
    Public Function ShowCalendar(Optional ByVal XDate As 
DateTime = Nothing) As Table
        Dim xtable As Table
        Dim xtablerow As TableRow
        Dim xtablecell As TableCell
        Dim ximage As Image
        Dim xtextbox As TextBox
        Dim xcalendar As CalendarExtender

        ximage = New Image
        xtextbox = New TextBox
        xcalendar = New CalendarExtender

        _image = ximage
        _textbox = xtextbox
        _calendar = xcalendar
        _date = XDate

        'Assign values for the first time
        AssignDefaultValues()

        
System.Threading.Thread.CurrentThread.CurrentCulture = New 
System.Globalization.CultureInfo("en-US")

        _randomnum = Date.Now.Minute.ToString & 
Date.Now.Millisecond.ToString & _random.Next(1, 
1000000).ToString()

        _image.ImageUrl = "~/Images/Controls/Calendar.png"
        _image.ImageAlign = ImageAlign.AbsMiddle
        _image.Style.Add("Cursor", "hand")
       [COLOR="Red"] _image.ID = "ImageCalendar" & 
_randomnum[/QUOTE]
        _textbox.ID = "TextBoxCalendar" & _randomnum
        _calendar.TargetControlID = _textbox.ID
        _calendar.PopupButtonID = _image.ID
        _calendar.Format = "dd/MMMM/yyyy"
        _textbox.BorderWidth = 0
        _textbox.ReadOnly = True
        _textbox.Style.Add("cursor", "default")
        _textbox.Style.Add("text-transform", "capitalize")
        _textbox.Attributes.Add("onselectstart", "return 
false")
        _textbox.ReadOnly = True
        _textbox.BackColor = _backcolor
        _textbox.ForeColor = _forecolor

        xtable = New Table
        xtable.BackColor = _backcolor
        xtable.BorderWidth = 0
        xtable.CellPadding = 0
        xtable.CellSpacing = 0

        xtablerow = New TableRow
        xtablerow.BorderWidth = 0

        xtablecell = New TableCell
        xtablecell.BorderWidth = 0
        xtablecell.Width = 30
        xtablecell.Controls.Add(_image)
        xtablerow.Cells.Add(xtablecell)

        xtablecell = New TableCell
        xtablecell.BorderWidth = 0
        xtablecell.Controls.Add(_textbox)
        xtablecell.Controls.Add(_calendar)

        xtablerow.Cells.Add(xtablecell)
        xtable.Rows.Add(xtablerow)

        Return xtable
    End Function

    ''' <summary>
    ''' Assing Default values
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AssignDefaultValues()
        If _defaulttext = Nothing Then
            _defaulttext = "(Select)"
        End If

        If _backcolor = Nothing Then
            _backcolor = Drawing.Color.White
        End If

        If _forecolor = Nothing Then
            _forecolor = Drawing.Color.Black
        End If

        If _date = Nothing Then
            _textbox.Text = _defaulttext
        Else
            _textbox.Text = _date
            _calendar.SelectedDate = _date
        End If

        'Validate Date
        _onclientdateselectionchanged = "function 
CalendarCheckDate(sender,args) { "
        _onclientdateselectionchanged = 
_onclientdateselectionchanged & "if (sender._selectedDate > 
new Date())"
        _onclientdateselectionchanged = 
_onclientdateselectionchanged & "{"
        _onclientdateselectionchanged = 
_onclientdateselectionchanged & "alert('Error:\n- The date 
cannot be higher than today');"
        _onclientdateselectionchanged = 
_onclientdateselectionchanged & "sender._selectedDate = new 
Date();"
        _onclientdateselectionchanged = 
_onclientdateselectionchanged & 
"sender._textbox.set_Value(sender._selectedDate.format(send
er._format));"
        _onclientdateselectionchanged = 
_onclientdateselectionchanged & "}"
        _onclientdateselectionchanged = 
_onclientdateselectionchanged & "}"

        _calendar.OnClientDateSelectionChanged = 
_onclientdateselectionchanged
    End Sub
End Class

Page.aspx
Code:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" 
runat="server" />
        <asp:Label ID="LabelCalendar1" runat="server" 
Text=""></asp:Label><br />
        <asp:Label ID="LabelCalendar2" runat="server" 
Text=""></asp:Label>
    </div>
    </form>
</body>

Page.vb
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e 
As System.EventArgs) Handles Me.Load
        Dim Calendar1 As New XCalendar
        
LabelCalendar1.Controls.Add(Calendar1.ShowCalendar(Date.Now
))

        Dim calendar2 As New XCalendar
        
LabelCalendar2.Controls.Add(calendar2.ShowCalendar(Date.Par
se("2000-05-30")))
    End Sub


When I execute Page.aspx, throw the error in the line
highlight in red

What Can I do?? (Really sorry for my bad English)


thx for the help! :D
 
H

Hans Kesting

anr pretended :
Hi all,

I have a problem with my XCalendar custom class. If I put
in the page one instance it works, but if i put two or more
throw an error like this:

"Multiple controls with the same ID".

The class is contained basically by a Textbox and
CalendarExtender control. And the error is is the
hightlighted line.

VB CLASS
Code:
Imports Microsoft.VisualBasic
Imports AjaxControlToolkit

Public Class XCalendar [snip]

What Can I do?? (Really sorry for my bad English)


thx for the help! :D
[/QUOTE]

Implement the INamingContainer interface. You don't need to do anything 
special other than mention that is *does* implement that interface.

Shouldn't this control at least derive from "Control"?

Hans Kesting
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top