Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • 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.
0
13579288
Asked:
13579288
1 Solution
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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