Hello All.
I have created my first custom web control to use client
side script to move items from one list box to another
(thanks Andy Smith for the inspiration even though I
couldnt understand the C# code).
The only way I could get the items in the boxes to persist
accross postbacks was to concat the lists in to a string
and write the string , via a hidden field, to the
viewstate.
Then on re-rendering I have to un-concat the string back
into its elements.
I would be happier if I could write a more complex object
like a ListBox object to the viewstate.
Is this possible?
Ideas and comments gratefull accepted.
Lance
Code so far .....
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.Collections.Specialized
<DefaultProperty("Text"), ToolboxData("<{0}:LancesListBox
runat=server></{0}:LancesListBox>")> _
Public Class LancesListBox
Inherits WebControl
Implements INamingContainer
Implements IPostBackDataHandler
Dim vDropListItems As New ListItemCollection()
Dim vListItems As New ListItemCollection()
Public Property DropListItems() As ListItemCollection
Get
vDropListItems.Clear()
Dim StoredValues As String = CType(ViewState
(UniqueID & "_DropList_Storage"), String)
If Not StoredValues = "" Then
Dim aryItems() As String
Dim i As Int16
aryItems = Split(StoredValues, "@@")
For i = 1 To aryItems.Length - 1
Dim aryValues() As String
aryValues = Split(aryItems(i), "//", 2)
Dim li As New ListItem(aryValues(0),
aryValues(1))
vDropListItems.Add(li)
Next
End If
Return vDropListItems
End Get
Set(ByVal Value As ListItemCollection)
Dim strStorage As String
Dim li As New ListItem()
For Each li In Value
strStorage = strStorage & "@@" & li.Text
& "//" & li.Value
Next
ViewState(UniqueID & "_DropList_Storage") =
strStorage
End Set
End Property
Public Property ListItems() As ListItemCollection
Get
vListItems.Clear()
Dim StoredValues As String = CType(ViewState
(UniqueID & "_List_Storage"), String)
If Not StoredValues = "" Then
Dim aryItems() As String
Dim i As Int16
aryItems = Split(StoredValues, "@@")
For i = 1 To aryItems.Length - 1
Dim aryValues() As String
aryValues = Split(aryItems(i), "//", 2)
Dim li As New ListItem(aryValues(0),
aryValues(1))
vListItems.Add(li)
Next
End If
Return vListItems
End Get
Set(ByVal Value As ListItemCollection)
Dim strStorage As String
Dim li As New ListItem()
For Each li In Value
strStorage = strStorage & "@@" & li.Text
& "//" & li.Value
Next
ViewState(UniqueID & "_List_Storage") =
strStorage
End Set
End Property
Protected Overrides Sub OnPreRender(ByVal e As
System.EventArgs)
Page.RegisterRequiresPostBack(Me)
'Send the Java script to the client
Dim strScript As String = GetJavaScripts()
'check to see if a copy has already been sent
'if there is more than one instance of the control
on the page
If (Not Page.IsClientScriptBlockRegistered
("LancesListBoxScript")) Then
Page.RegisterClientScriptBlock
("LancesListBoxScript", strScript)
End If
End Sub
Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
'The table to hold the controls
output.RenderBeginTag("table")
output.RenderBeginTag("tr")
output.RenderBeginTag("td")
'the drop down list
output.AddAttribute("name", Me.UniqueID
& "_DropList")
output.AddAttribute("style", "width:120")
output.RenderBeginTag("select")
'add items to the DropDownList
'This also repopulates the list
'from info in the ViewState during postbacks
Dim li As New ListItem()
For Each li In DropListItems
output.AddAttribute("value", li.Value)
output.RenderBeginTag("option")
output.Write(li.Text)
output.RenderEndTag() '</option>
Next
output.RenderEndTag() '</select>
'the dropdown storage
'this is a hidden field to persist dropdownList
info
'During postback it reloads itself from the
viewstate
output.AddAttribute("name", Me.UniqueID
& "_DropList_Storage")
output.AddAttribute("type", "hidden")
output.AddAttribute("value", CType(ViewState
(UniqueID & "_DropList_Storage"), String))
output.RenderBeginTag("input")
output.RenderEndTag()
'The add button
output.AddAttribute("name", Me.UniqueID & "_Add")
output.AddAttribute("type", "button")
output.AddAttribute("style", "width:22;Height:22")
output.AddAttribute("onclick", "moveDualList( " &
UniqueID & "_DropList, " & UniqueID & "_List, " & UniqueID
& "_DropList_Storage, " & UniqueID & "_List_Storage, " &
UniqueID & "_DropList, " & UniqueID & "_List )")
output.AddAttribute("value", "+")
output.RenderBeginTag("input")
output.RenderEndTag() '</input>
output.RenderEndTag() '</td>
output.RenderEndTag() '</tr>
output.RenderBeginTag("tr")
output.RenderBeginTag("td")
'the list
output.AddAttribute("name", Me.UniqueID & "_List")
output.AddAttribute("style", "width:120")
output.AddAttribute("size", "4")
output.RenderBeginTag("select")
'add items to the ListBox
'This also repopulates the list
'from info in the ViewState during postbacks
For Each li In ListItems
output.AddAttribute("value", li.Value)
output.RenderBeginTag("option")
output.Write(li.Text)
output.RenderEndTag() '</option>
Next
output.RenderEndTag() '</select>
'the list storage
'this is a hidden field to persist List info
'During postback it reloads itself from the
viewstate
output.AddAttribute("name", Me.UniqueID
& "_List_Storage")
output.AddAttribute("type", "hidden")
output.AddAttribute("value", CType(ViewState
(UniqueID & "_List_Storage"), String))
output.RenderBeginTag("input")
output.RenderEndTag()