Still having collection issues

Discussion in 'ASP .Net Building Controls' started by Stanley, Feb 2, 2004.

  1. Stanley

    Stanley Guest

    Ok below is the code to my control with some code parts removed to keep this
    post from being a novel. My problem is at this point I can add new items
    from my collection and remove them via teh GUI designer at design time but
    the information I put in at that time is not persisted to runtime. What am I
    doing wrong?

    -Stanley

    Code:
    Imports System.ComponentModel
    Imports System.ComponentModel.Design
    Imports System.Drawing.Design
    Imports System.Web.UI
    
    <ToolboxData("<{0}:myControl runat=server></{0}:myControl>"), _
    Designer(GetType(c)), _
    PersistChildren(False), _
    ParseChildren(False), _
    DefaultProperty("Buttons")> _
    Public Class myControl
    Inherits System.Web.UI.Control
    
    Private _buttons As ButtonCollection
    
    <Editor(GetType(myCollectionEditor), GetType(UITypeEditor)),
    NotifyParentProperty(True),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    PersistenceMode(PersistenceMode.InnerDefaultProperty)> _
    Public ReadOnly Property Buttons() As ButtonCollection
    Get
    If _buttons Is Nothing Then
    _buttons = New ButtonCollection
    End If
    Return _buttons
    End Get
    End Property
    
    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)
    Me.EnsureChildControls()
    Me.RenderChildren(output)
    End Sub
    Protected Overrides Sub CreateChildControls()
    Dim script As New System.Text.StringBuilder
    
    '==================================
    ' Code left out because of length
    '==================================
    
    If Not Page.IsClientScriptBlockRegistered(Me.ID.ToString &
    "_myScript") Then
    Page.RegisterClientScriptBlock(Me.ID.ToString & "_myScript",
    script.ToString)
    End If
    
    Dim script1 As New System.Text.StringBuilder
    
    '==================================
    ' Code left out because of length
    '==================================
    
    If Not Page.IsClientScriptBlockRegistered(Me.ID.ToString &
    "_myStartup") Then
    Page.RegisterClientScriptBlock(Me.ID.ToString & "_myStartup",
    script1.ToString)
    End If
    Dim b As ButtonClass
    If Not _buttons Is Nothing Then
    For Each b In _buttons
    If b.ButtonType = ButtonClass.ButtonTypes.Button Then
    Dim _b As WebControls.Button =
    CType(Page.FindControl(b.ButtonName), WebControls.Button)
    _b.Attributes.Add("onclick", "Javascript:b=true;")
    ElseIf b.ButtonType = ButtonClass.ButtonTypes.ImageButton
    Then
    Dim _b As WebControls.ImageButton =
    CType(Page.FindControl(b.ButtonName), WebControls.ImageButton)
    _b.Attributes.Add("onclick", "Javascript:b=true;")
    ElseIf b.ButtonType = ButtonClass.ButtonTypes.LinkButton
    Then
    Dim _b As WebControls.LinkButton =
    CType(Page.FindControl(b.ButtonName), WebControls.LinkButton)
    _b.Attributes.Add("onclick", "Javascript:b=true;")
    End If
    Next
    End If
    End Sub
    End Class
    
    Public Class ButtonClass
    'Inherits Control
    Public Enum ButtonTypes
    LinkButton
    Button
    ImageButton
    End Enum
    Private _id As String
    Private _buttontype As ButtonTypes
    
    Public Property ButtonName() As String
    Get
    Return _id
    End Get
    Set(ByVal Value As String)
    _id = Value
    End Set
    End Property
    
    Public Property ButtonType() As ButtonTypes
    Get
    Return _buttontype
    End Get
    Set(ByVal Value As ButtonTypes)
    _buttontype = Value
    End Set
    End Property
    
    End Class
    
    Public Class ButtonCollection
    Inherits CollectionBase
    Private _buttons As ArrayList
    
    Friend Sub New()
    If _buttons Is Nothing Then
    _buttons = New ArrayList
    End If
    End Sub
    
    Public ReadOnly Property Button(ByVal nIndex As Integer)
    Get
    Return CType(_buttons(nIndex), ButtonClass)
    End Get
    End Property
    
    Public Sub Add(ByVal button As ButtonClass)
    _buttons.Add(button)
    End Sub
    
    Public Function IndexOf(ByVal button As ButtonClass)
    Return _buttons.IndexOf(button)
    End Function
    End Class
    
    
    
    Public Class myCollectionEditor
    Inherits CollectionEditor
    
    
    Public Sub New(ByVal type As Type)
    MyBase.New(type)
    End Sub
    
    
    Protected Overrides Function CreateCollectionItemType() As Type
    Return GetType(ButtonClass)
    End Function
    End Class
    
    Public Class myControl_Designer
    Inherits System.Web.UI.Design.ControlDesigner
    Protected Overrides Function GetEmptyDesignTimeHtml() As String
    Dim strHtml As String
    '==================================
    ' Code left out because of length
    '==================================
    Return strHtml
    End Function
    
    Public Overrides Function GetDesignTimeHtml() As String
    Dim strHtml As String
    '==================================
    ' Code left out because of length
    '==================================
    Return strHtml
    End Function
    End Class
    
     
    Stanley, Feb 2, 2004
    #1
    1. Advertising

  2. Stanley

    gozza Guest

    Just a guess here?

    [ParseChildren(true, "Buttons")]

    gozza

    "Stanley" <> wrote in message
    news:...
    > Ok below is the code to my control with some code parts removed to keep

    this
    > post from being a novel. My problem is at this point I can add new items
    > from my collection and remove them via teh GUI designer at design time but
    > the information I put in at that time is not persisted to runtime. What am

    I
    > doing wrong?
    >
    > -Stanley
    >
    >
    Code:
    > Imports System.ComponentModel
    > Imports System.ComponentModel.Design
    > Imports System.Drawing.Design
    > Imports System.Web.UI
    >
    > <ToolboxData("<{0}:myControl runat=server></{0}:myControl>"), _
    > Designer(GetType(c)), _
    > PersistChildren(False), _
    > ParseChildren(False), _
    > DefaultProperty("Buttons")> _
    > Public Class myControl
    >     Inherits System.Web.UI.Control
    >
    >     Private _buttons As ButtonCollection
    >
    >     <Editor(GetType(myCollectionEditor), GetType(UITypeEditor)),
    > NotifyParentProperty(True),
    > DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    > PersistenceMode(PersistenceMode.InnerDefaultProperty)> _
    >     Public ReadOnly Property Buttons() As ButtonCollection
    >         Get
    >             If _buttons Is Nothing Then
    >                 _buttons = New ButtonCollection
    >             End If
    >             Return _buttons
    >         End Get
    >     End Property
    >
    >     Protected Overrides Sub Render(ByVal output As
    > System.Web.UI.HtmlTextWriter)
    >         Me.EnsureChildControls()
    >         Me.RenderChildren(output)
    >     End Sub
    >     Protected Overrides Sub CreateChildControls()
    >         Dim script As New System.Text.StringBuilder
    >
    >  '==================================
    >         ' Code left out because of length
    >  '==================================
    >
    >         If Not Page.IsClientScriptBlockRegistered(Me.ID.ToString &
    > "_myScript") Then
    >             Page.RegisterClientScriptBlock(Me.ID.ToString & "_myScript",
    > script.ToString)
    >         End If
    >
    >         Dim script1 As New System.Text.StringBuilder
    >
    >  '==================================
    >         ' Code left out because of length
    >  '==================================
    >
    >         If Not Page.IsClientScriptBlockRegistered(Me.ID.ToString &
    > "_myStartup") Then
    >             Page.RegisterClientScriptBlock(Me.ID.ToString & "_myStartup",
    > script1.ToString)
    >         End If
    >         Dim b As ButtonClass
    >         If Not _buttons Is Nothing Then
    >             For Each b In _buttons
    >                 If b.ButtonType = ButtonClass.ButtonTypes.Button Then
    >                     Dim _b As WebControls.Button =
    > CType(Page.FindControl(b.ButtonName), WebControls.Button)
    >                     _b.Attributes.Add("onclick", "Javascript:b=true;")
    >                 ElseIf b.ButtonType = ButtonClass.ButtonTypes.ImageButton
    > Then
    >                     Dim _b As WebControls.ImageButton =
    > CType(Page.FindControl(b.ButtonName), WebControls.ImageButton)
    >                     _b.Attributes.Add("onclick", "Javascript:b=true;")
    >                 ElseIf b.ButtonType = ButtonClass.ButtonTypes.LinkButton
    > Then
    >                     Dim _b As WebControls.LinkButton =
    > CType(Page.FindControl(b.ButtonName), WebControls.LinkButton)
    >                     _b.Attributes.Add("onclick", "Javascript:b=true;")
    >                 End If
    >             Next
    >         End If
    >     End Sub
    > End Class
    >
    > Public Class ButtonClass
    >     'Inherits Control
    >     Public Enum ButtonTypes
    >         LinkButton
    >         Button
    >         ImageButton
    >     End Enum
    >     Private _id As String
    >     Private _buttontype As ButtonTypes
    >
    >     Public Property ButtonName() As String
    >         Get
    >             Return _id
    >         End Get
    >         Set(ByVal Value As String)
    >             _id = Value
    >         End Set
    >     End Property
    >
    >     Public Property ButtonType() As ButtonTypes
    >         Get
    >             Return _buttontype
    >         End Get
    >         Set(ByVal Value As ButtonTypes)
    >             _buttontype = Value
    >         End Set
    >     End Property
    >
    > End Class
    >
    > Public Class ButtonCollection
    >     Inherits CollectionBase
    >     Private _buttons As ArrayList
    >
    >     Friend Sub New()
    >         If _buttons Is Nothing Then
    >             _buttons = New ArrayList
    >         End If
    >     End Sub
    >
    >     Public ReadOnly Property Button(ByVal nIndex As Integer)
    >         Get
    >             Return CType(_buttons(nIndex), ButtonClass)
    >         End Get
    >     End Property
    >
    >     Public Sub Add(ByVal button As ButtonClass)
    >         _buttons.Add(button)
    >     End Sub
    >
    >     Public Function IndexOf(ByVal button As ButtonClass)
    >         Return _buttons.IndexOf(button)
    >     End Function
    > End Class
    >
    >
    >
    > Public Class myCollectionEditor
    >     Inherits CollectionEditor
    >
    >
    >     Public Sub New(ByVal type As Type)
    >         MyBase.New(type)
    >     End Sub
    >
    >
    >     Protected Overrides Function CreateCollectionItemType() As Type
    >         Return GetType(ButtonClass)
    >     End Function
    > End Class
    >
    > Public Class myControl_Designer
    >     Inherits System.Web.UI.Design.ControlDesigner
    >     Protected Overrides Function GetEmptyDesignTimeHtml() As String
    >         Dim strHtml As String
    >  '==================================
    >         ' Code left out because of length
    >  '==================================
    >         Return strHtml
    >     End Function
    >
    >     Public Overrides Function GetDesignTimeHtml() As String
    >         Dim strHtml As String
    >  '==================================
    >         ' Code left out because of length
    >  '==================================
    >         Return strHtml
    >     End Function
    > End Class
    > 
    >
    >
     
    gozza, Feb 3, 2004
    #2
    1. Advertising

  3. Stanley

    Stanley Guest

    Tried that and I still am having the problem. The basic idea is that I can
    click on the elipses in the designer to add the ID of buttons on my page
    that I want the attributes changed. I also have it so that they can pick
    from 3 different types which are LinkButton, Button and ImageButton. Then
    when the control renders it will look on the page and find those buttons and
    add the attribute to them. Does anyone have some source code to work with
    collections like this in a custom control? I just need to look at an example
    to see where I am going wrong. Or perhaps a tutorial? Over a week of
    searching and I havn't found one yet.

    TIA

    -Stanley

    "gozza" <> wrote in message
    news:...
    > Just a guess here?
    >
    > [ParseChildren(true, "Buttons")]
    >
    > gozza
    >
    > "Stanley" <> wrote in message
    > news:...
    > > Ok below is the code to my control with some code parts removed to keep

    > this
    > > post from being a novel. My problem is at this point I can add new items
    > > from my collection and remove them via teh GUI designer at design time

    but
    > > the information I put in at that time is not persisted to runtime. What

    am
    > I
    > > doing wrong?
    > >
    > > -Stanley
    > >
    > >
    Code:
    > > Imports System.ComponentModel
    > > Imports System.ComponentModel.Design
    > > Imports System.Drawing.Design
    > > Imports System.Web.UI
    > >
    > > <ToolboxData("<{0}:myControl runat=server></{0}:myControl>"), _
    > > Designer(GetType(c)), _
    > > PersistChildren(False), _
    > > ParseChildren(False), _
    > > DefaultProperty("Buttons")> _
    > > Public Class myControl
    > >     Inherits System.Web.UI.Control
    > >
    > >     Private _buttons As ButtonCollection
    > >
    > >     <Editor(GetType(myCollectionEditor), GetType(UITypeEditor)),
    > > NotifyParentProperty(True),
    > >[/color][/color]
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content),[color=blue][color=green]
    > > PersistenceMode(PersistenceMode.InnerDefaultProperty)> _
    > >     Public ReadOnly Property Buttons() As ButtonCollection
    > >         Get
    > >             If _buttons Is Nothing Then
    > >                 _buttons = New ButtonCollection
    > >             End If
    > >             Return _buttons
    > >         End Get
    > >     End Property
    > >
    > >     Protected Overrides Sub Render(ByVal output As
    > > System.Web.UI.HtmlTextWriter)
    > >         Me.EnsureChildControls()
    > >         Me.RenderChildren(output)
    > >     End Sub
    > >     Protected Overrides Sub CreateChildControls()
    > >         Dim script As New System.Text.StringBuilder
    > >
    > >  '==================================
    > >         ' Code left out because of length
    > >  '==================================
    > >
    > >         If Not Page.IsClientScriptBlockRegistered(Me.ID.ToString &
    > > "_myScript") Then
    > >             Page.RegisterClientScriptBlock(Me.ID.ToString & "_myScript",
    > > script.ToString)
    > >         End If
    > >
    > >         Dim script1 As New System.Text.StringBuilder
    > >
    > >  '==================================
    > >         ' Code left out because of length
    > >  '==================================
    > >
    > >         If Not Page.IsClientScriptBlockRegistered(Me.ID.ToString &
    > > "_myStartup") Then
    > >             Page.RegisterClientScriptBlock(Me.ID.ToString &[/color][/color]
    "_myStartup",[color=blue][color=green]
    > > script1.ToString)
    > >         End If
    > >         Dim b As ButtonClass
    > >         If Not _buttons Is Nothing Then
    > >             For Each b In _buttons
    > >                 If b.ButtonType = ButtonClass.ButtonTypes.Button Then
    > >                     Dim _b As WebControls.Button =
    > > CType(Page.FindControl(b.ButtonName), WebControls.Button)
    > >                     _b.Attributes.Add("onclick", "Javascript:b=true;")
    > >                 ElseIf b.ButtonType =[/color][/color]
    ButtonClass.ButtonTypes.ImageButton[color=blue][color=green]
    > > Then
    > >                     Dim _b As WebControls.ImageButton =
    > > CType(Page.FindControl(b.ButtonName), WebControls.ImageButton)
    > >                     _b.Attributes.Add("onclick", "Javascript:b=true;")
    > >                 ElseIf b.ButtonType = ButtonClass.ButtonTypes.LinkButton
    > > Then
    > >                     Dim _b As WebControls.LinkButton =
    > > CType(Page.FindControl(b.ButtonName), WebControls.LinkButton)
    > >                     _b.Attributes.Add("onclick", "Javascript:b=true;")
    > >                 End If
    > >             Next
    > >         End If
    > >     End Sub
    > > End Class
    > >
    > > Public Class ButtonClass
    > >     'Inherits Control
    > >     Public Enum ButtonTypes
    > >         LinkButton
    > >         Button
    > >         ImageButton
    > >     End Enum
    > >     Private _id As String
    > >     Private _buttontype As ButtonTypes
    > >
    > >     Public Property ButtonName() As String
    > >         Get
    > >             Return _id
    > >         End Get
    > >         Set(ByVal Value As String)
    > >             _id = Value
    > >         End Set
    > >     End Property
    > >
    > >     Public Property ButtonType() As ButtonTypes
    > >         Get
    > >             Return _buttontype
    > >         End Get
    > >         Set(ByVal Value As ButtonTypes)
    > >             _buttontype = Value
    > >         End Set
    > >     End Property
    > >
    > > End Class
    > >
    > > Public Class ButtonCollection
    > >     Inherits CollectionBase
    > >     Private _buttons As ArrayList
    > >
    > >     Friend Sub New()
    > >         If _buttons Is Nothing Then
    > >             _buttons = New ArrayList
    > >         End If
    > >     End Sub
    > >
    > >     Public ReadOnly Property Button(ByVal nIndex As Integer)
    > >         Get
    > >             Return CType(_buttons(nIndex), ButtonClass)
    > >         End Get
    > >     End Property
    > >
    > >     Public Sub Add(ByVal button As ButtonClass)
    > >         _buttons.Add(button)
    > >     End Sub
    > >
    > >     Public Function IndexOf(ByVal button As ButtonClass)
    > >         Return _buttons.IndexOf(button)
    > >     End Function
    > > End Class
    > >
    > >
    > >
    > > Public Class myCollectionEditor
    > >     Inherits CollectionEditor
    > >
    > >
    > >     Public Sub New(ByVal type As Type)
    > >         MyBase.New(type)
    > >     End Sub
    > >
    > >
    > >     Protected Overrides Function CreateCollectionItemType() As Type
    > >         Return GetType(ButtonClass)
    > >     End Function
    > > End Class
    > >
    > > Public Class myControl_Designer
    > >     Inherits System.Web.UI.Design.ControlDesigner
    > >     Protected Overrides Function GetEmptyDesignTimeHtml() As String
    > >         Dim strHtml As String
    > >  '==================================
    > >         ' Code left out because of length
    > >  '==================================
    > >         Return strHtml
    > >     End Function
    > >
    > >     Public Overrides Function GetDesignTimeHtml() As String
    > >         Dim strHtml As String
    > >  '==================================
    > >         ' Code left out because of length
    > >  '==================================
    > >         Return strHtml
    > >     End Function
    > > End Class
    > > 
    > >
    > >

    >
    >
     
    Stanley, Feb 3, 2004
    #3
    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. Steve

    Still having postback problems

    Steve, Jan 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    330
    Steve
    Jan 11, 2004
  2. Brent White
    Replies:
    8
    Views:
    632
    S. Justin Gengo
    Oct 26, 2005
  3. darrel
    Replies:
    0
    Views:
    351
    darrel
    May 4, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    981
    Øyvind Isaksen
    May 18, 2007
  5. Stanley

    Still working on my collection issues

    Stanley, Feb 4, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    208
    Stanley
    Feb 4, 2004
Loading...

Share This Page