Still having collection issues

S

Stanley

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
 
G

gozza

Just a guess here?

[ParseChildren(true, "Buttons")]

gozza

Stanley said:
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
 
S

Stanley

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 said:
Just a guess here?

[ParseChildren(true, "Buttons")]

gozza

Stanley said:
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
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top