Problem with a List2List control

L

lisa

Please, I hope someone can help me with this.

I'm trying to make a simple List2List control. I want to expose both
listboxes and both buttons (add, remove) fully, so that the user can
access their properties (and so that I don't have to choose which
properties to expose explicitly).

What I want to get is something like this:

<ll:pickList id="PickList1" runat="server">
<ll:LeftList>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</ll:LeftList>
<ll:MoveLeft />
<ll:MoveRight />
<ll:RightList>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</ll:RightList>
</ll:pickList>

What I'm getting instead is this:

<ll:pickList id="PickList1" runat="server"></ll:pickList>

I can add items to the lists in design view in the collection editor,
but they don't show up in the render until I go to HTML view and then
come back again. And when I run a page where I've added items to one
of the listboxes, they disappear altogether. When I ran it in debug
mode, I saw that it was reinstantiating the controls at runtime.

I would have thought that the PersistenceMode.InnerProperty would let
the child controls show in the HTML view, and I would have thought that
the NotifyParentProperty would cause the design view to update when I
added items. And I have no idea why it's reinstantiating.

I tried adding custom State management methods, hoping that putting the
state of the child controls into viewstate would help. It doesn't seem
to have made any difference at all.

Can someone please help? This looks like it should be so simple, and
it's driving me crazy.

Thanks,
Lisa

Option Strict On
Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO

<DefaultProperty("SelectedIndex"), Description("A List2List control
that uses ListBox"), ToolboxData("<{0}:pickList
runat=server></{0}:pickList>"), ParseChildren(True),
PersistChildren(True)> _
Public Class PickList
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer

Private _LeftList As ListBox
Private _RightList As ListBox
Private _MoveLeft As Button
Private _MoveRight As Button

Protected Overrides Sub CreateChildControls()

Controls.Clear()

_LeftList = New ListBox
_LeftList.ID = "LeftList"

_RightList = New ListBox
_RightList.ID = "RightList"

_MoveLeft = New Button
_MoveLeft.ID = "MoveList"
_MoveLeft.Width = Unit.Pixel(25)
_MoveLeft.Font.Name = "wingdings"
_MoveLeft.Text = "×"

_MoveRight = New Button
_MoveRight.ID = "MoveRight"
_MoveRight.Width = Unit.Pixel(25)
_MoveRight.Font.Name = "wingdings"
_MoveRight.Text = "Ø"

Controls.Add(_LeftList)
Controls.Add(_RightList)
Controls.Add(_MoveLeft)
Controls.Add(_MoveRight)

End Sub 'CreateChildControls

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)

EnsureChildControls()
AddAttributesToRender(writer)

writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0")
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0")
writer.AddStyleAttribute("display", "inline")
writer.RenderBeginTag(HtmlTextWriterTag.Table)
writer.RenderBeginTag(HtmlTextWriterTag.Tr)

writer.RenderBeginTag(HtmlTextWriterTag.Td)
_LeftList.RenderControl(writer)
writer.RenderEndTag() 'td

writer.RenderBeginTag(HtmlTextWriterTag.Td)
_MoveLeft.RenderControl(writer)
_MoveRight.RenderControl(writer)
writer.RenderEndTag() 'td

writer.RenderBeginTag(HtmlTextWriterTag.Td)
_RightList.RenderControl(writer)
writer.RenderEndTag() 'td

writer.RenderEndTag() 'tr
writer.RenderEndTag() 'table

End Sub 'Render

#Region "Child controls exposed fully as properties"

Public Overrides ReadOnly Property Controls() As
System.Web.UI.ControlCollection
Get
EnsureChildControls()
Return MyBase.Controls
End Get
End Property 'Controls

<Category("Child Controls"), Bindable(False),
NotifyParentProperty(True),
PersistenceMode(PersistenceMode.InnerProperty)> _
Public ReadOnly Property LeftList() As ListBox
Get
Me.EnsureChildControls()
Return _LeftList
End Get
End Property 'LeftList

<Category("Child Controls"), Bindable(False),
NotifyParentProperty(True),
PersistenceMode(PersistenceMode.InnerProperty)> _
Public ReadOnly Property RightList() As ListBox
Get
Me.EnsureChildControls()
Return _RightList
End Get
End Property 'RightList

<Category("Child Controls"), Bindable(False),
NotifyParentProperty(True),
PersistenceMode(PersistenceMode.InnerProperty)> _
Public ReadOnly Property MoveLeft() As Button
Get
Me.EnsureChildControls()
Return _MoveLeft
End Get
End Property 'MoveLeft

<Category("Child Controls"), Bindable(False),
NotifyParentProperty(True),
PersistenceMode(PersistenceMode.InnerProperty)> _
Public ReadOnly Property MoveRight() As Button
Get
Me.EnsureChildControls()
Return _MoveRight
End Get
End Property 'MoveRight

#End Region

#Region "Custom state management for child controls"

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
Dim MyState() As Object = CType(savedState, Object())
MyBase.LoadViewState(MyState(0))
If Not MyState(1) Is Nothing Then CType(_LeftList,
IStateManager).LoadViewState(MyState(1))
If Not MyState(2) Is Nothing Then CType(_RightList,
IStateManager).LoadViewState(MyState(1))
If Not MyState(3) Is Nothing Then CType(_MoveLeft,
IStateManager).LoadViewState(MyState(1))
If Not MyState(4) Is Nothing Then CType(_MoveRight,
IStateManager).LoadViewState(MyState(1))
End Sub 'LoadViewState

Protected Overrides Function SaveViewState() As Object
Dim MyState(4) As Object
MyState(0) = MyBase.SaveViewState
MyState(1) = IIf(_LeftList Is Nothing, Nothing,
CType(_LeftList, IStateManager).SaveViewState)
MyState(2) = IIf(_RightList Is Nothing, Nothing,
CType(_RightList, IStateManager).SaveViewState)
MyState(3) = IIf(_MoveLeft Is Nothing, Nothing,
CType(_MoveLeft, IStateManager).SaveViewState)
MyState(4) = IIf(_MoveRight Is Nothing, Nothing,
CType(_MoveRight, IStateManager).SaveViewState)
Return MyState
End Function 'SaveViewState

Protected Overrides Sub TrackViewState()
MyBase.TrackViewState()
If Not _LeftList Is Nothing Then CType(_LeftList,
IStateManager).TrackViewState()
If Not _RightList Is Nothing Then CType(_RightList,
IStateManager).TrackViewState()
If Not _MoveLeft Is Nothing Then CType(_MoveLeft,
IStateManager).TrackViewState()
If Not _MoveRight Is Nothing Then CType(_MoveRight,
IStateManager).TrackViewState()
End Sub 'TrackViewState

#End Region

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top