listbox/session linking

I'm using asp.net 3.5 and VB. I need sample VB coding of how to initially load a listbox from a session, and how to save the contents of a listbox to a session. The session will persist multiple times using the same aspx without a postback. It is used to build a parameterized query.
13579288Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GreymanMSCCommented:
Spool the items into an array and store the array into the session,
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ListBoxInSession.aspx.vb" Inherits="ListBoxInSession.ListBoxInSession" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>List Box In Session Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID=pnlAdder runat=server DefaultButton=btnAdder>
        <asp:TextBox ID=txtAdder runat=server ></asp:TextBox> 
        <asp:Button ID=btnAdder runat=server Text="Add" />
    </asp:Panel>
    <asp:ListBox ID=lbItems runat=server ></asp:ListBox>
    <asp:Button ID=btnRemove runat=server Text="Remove" />
    <asp:Button ID=btnCleaner runat=server Text="Clear" />
    <asp:Button ID=btnSaver runat=server Text="Save" />
    <asp:Button ID=btnLoader runat=server Text="Load" />
    </div>
    </form>
</body>
</html>
 
---
Namespace ListBoxInSession
Partial Class ListBoxInSession
    Inherits System.Web.UI.Page
 
    Const sSessionListItems As String = "ListBoxInSession_ListBoxInSession.ListItemsArray"
 
    Protected Sub btnAdder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdder.Click
        Dim sText As String = Me.txtAdder.Text
        Dim oListItems As ListItemCollection = Me.lbItems.Items
        If oListItems.FindByValue(sText) Is Nothing Then
            Dim oListItem As New ListItem(sText, sText)
            oListItems.Add(oListItem)
        End If
        Me.txtAdder.Text = Nothing
    End Sub
 
    Protected Sub btnSaver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaver.Click
        Dim oListItems As ListItemCollection = Me.lbItems.Items
        Dim aListItems() As ListItem = New ListItem(oListItems.Count - 1) {}
        oListItems.CopyTo(aListItems, 0)
        Session(sSessionListItems) = aListItems
    End Sub
 
    Protected Sub btnLoader_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoader.Click
        Dim aListItems() As ListItem = Session(sSessionListItems)
        If Not aListItems Is Nothing Then
            Dim oListItems As ListItemCollection = Me.lbItems.Items
            oListItems.Clear()
            For Each oListItem As ListItem In aListItems
                If Not oListItem Is Nothing Then
                    If oListItems.FindByValue(oListItem.Value) Is Nothing Then
                        oListItems.Add(oListItem)
                    End If
                End If
            Next
        End If
    End Sub
 
    Protected Sub btnCleaner_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCleaner.Click
        Me.lbItems.Items.Clear()
    End Sub
 
    Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        If Me.lbItems.SelectedIndex > -1 Then
            Dim oListItem As ListItem = Me.lbItems.SelectedItem
            Dim oListItems As ListItemCollection = Me.lbItems.Items
            oListItems.Remove(oListItem)
        End If
    End Sub
End Class
End Namespace

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.