• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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.
1 Solution
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>
    <form id="form1" runat="server">
    <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: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" />
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)
        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
            For Each oListItem As ListItem In aListItems
                If Not oListItem Is Nothing Then
                    If oListItems.FindByValue(oListItem.Value) Is Nothing Then
                    End If
                End If
        End If
    End Sub
    Protected Sub btnCleaner_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCleaner.Click
    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
        End If
    End Sub
End Class
End Namespace

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now