I am trying to create a custom button control for the web that, when clicked,
disables and changes the text of itself and a bunch of other controls (in the
collection). My goal is to end up with a button that prevents the user from
submitting a form back multiple times and also prevents them from submitting
the form and then clicking something else before the postback can finish
processing.
What I have is a working button that disables and changes the text of
itself. It generates the <input> tag and associated attributes and it
generates the javascript and the postback event and adds it to the tag. My
hope was to add a collection to the control to generate the javascript to
disable other controls and change their text as well. I wanted to be able to
add it to the properties window (similar to adding Items to a dropdownlist)
and use System.Web.UI.Design.WebControls.ListItemsCollecti onEditor to create
the editor. The problem I am having is adding the collection.
In an attempt to make this work I have added 3 additional classes (basing
this all off of a non-function example of how to create a custom
ListItemsCollectionEditor class I found on the web by Brian Mains). The
"ControlsToDisable" Class is the defines the object that the collection will
store. The "ControlsToDisableCollection" Class inherits from CollectionBase
and its only property is "Item". It also has a few functions for adding
items/ removing items/ etc. The "ControlsToDisableCollectionEditor" Class
(you like the naming convention?) inherits from the ListItemsCollectionEditor
class and sets the type of class to my "ControlsToDisableCollection" Class.
The "Button" Class then has a property that calls the collection. This works
in visual studio and allows me to use the editor to add items and stores
them... however when I run the page with the control on it and get a count of
items in the collection it's 0 and when I go back to the into visual studio
and open up the collection the list is empty. I was under the impression from
the article that the that the collection would be rendered on the page in the
HTML like items in a drop down are rendered, but they are not. I'm not sure
where they are getting stored while I'm in visual studio.
I am perfectly happy to go in a completely different direction with this if
that's what it takes, but I really want to be able to use the
ListItemsCollectionEditor class, as there will be a number of people using
this control and the familiarity means I don't have to repeatedly explain how
to use it. Now if you are still with me (I'm sorry) I'll put the classes
below in the order that I mentioned them. Some stuff has been left out to
help reduce the size of this posting. Thanks in advance for any help that you
can provide.
------------ControlsToDisable----------------------------------------
Imports System.Web.UI
Imports System.ComponentModel
Public Class ControlsToDisable
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Sub New()
'Do Nothing
End Sub
Public Sub New(ByVal Name As String)
_Name = Name
End Sub
Public Overrides Function tostring() As String
Return _Name
End Function
End Class
------------ControlsToDisableCollection------------------------------
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
<DefaultProperty("Item")> _
Public Class ControlsToDisableCollection
Inherits CollectionBase
<DesignerSerializationVisibility(DesignerSerializa tionVisibility.Visible), _
PersistenceMode(PersistenceMode.Attribute)> _
Default Public Property Item(ByVal intIndex As Integer) As
ButtonsToDisable
Get
Return DirectCast(list.Item(intIndex), ControlsToDisable)
End Get
Set(ByVal Value As ControlsToDisable)
list.Item(intIndex) = Value
End Set
End Property
Public Function Add(ByVal objItem As ControlsToDisable) As Integer
Return list.Add(objItem)
End Function
Public Function Add(ByVal Name As String) As Integer
Return list.Add(New ControlsToDisable(Name))
End Function
Public Function Contains(ByVal objItem As ControlsToDisable) As Boolean
Return list.Contains(objItem)
End Function
Public Sub Insert(ByVal intIndex As Integer, ByVal objItem As
ControlsToDisable)
list.Insert(intIndex, objItem)
End Sub
Public Sub Remove(ByVal objItem As ControlsToDisable)
list.Remove(objItem)
End Sub
End Class
------------ControlsToDisableCollectionEditor------------------------
Public Class ControlsToDisableCollectionEditor
Inherits System.Web.UI.Design.WebControls.ListItemsCollecti onEditor
Public Sub New()
MyBase.New(GetType(ControlsToDisableCollection))
End Sub
End Class
------------Button---------------------------------------------------
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Drawing
<ToolboxBitmap(GetType(System.Web.UI.WebControls.B utton)), _
DefaultEvent("Click"), DefaultProperty("Text"), _
ParseChildren(False), PersistChildren(True)> _
Public Class Button
Inherits System.Web.UI.WebControls.WebControl
Private _ControlListToDisable As New controlsToDisableCollection
<Bindable(True), Browsable(True), Category("Data"), _
Editor(GetType(controlsToDisableCollection),
GetType(System.Web.UI.Design.WebControls.ListItems CollectionEditor)), _
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content), _
PersistenceMode(PersistenceMode.InnerProperty)> _
Public Property ControlListToDisable() As ControlsToDisableCollection
Get
Return _ControlListToDisable
End Get
Set(ByVal Value As ControlsToDisableCollection)
_ControlListToDisable = Value
End Set
End Property
Public Sub New()
MyBase.New(HtmlTextWriterTag.Input)
End Sub
End Class