Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  Listboxes: Linked Dynamically from Database w/JavaScript
     [next Lesson]  Server Perlscript: Resources

Dual List Boxes by Bill Wilkinson   (billw@chilisoft.com)

One nice thing to do is allow users to use one list box and another to transfer elements elegantly. It makes for a friendly interface.

   filename=/learn/test/listdual.asp

<Test Script Below>



<html>
<body>

<%
    Dim Available
    Dim Chosen
    If Session("BeenHere") <> "DoneThat" Then
        ' First time to this page!
        ' 
        ' In "real life" you'd probably populate the
        ' "initialItems" array from a database query,
        ' but for demo purposes we'll do it this way:
        '
        initialItems = Array("Apples","Oranges","Grapes","Berries","Kiwis")
        '
        Set Available = CreateObject("Scripting.Dictionary")
        For i = 0 To UBound( initialItems )
            Available.add initialItems( i ), "no" ' no means not chosen
        Next
        '
        ' save for next time here
        '
        Set Session("Choices") = Available
        '
        ' set the flag so we don't do this again
        '
        Session("BeenHere") = "DoneThat"        

    Else ' if BeenHere *DOES* equal "DoneThat"...

        '
        ' Been here before...process requests...
        ' 
        ' First, retrieve what we remembered from before...
        '
        Set Available = Session("Choices")
        '
        ' including any error message?
        '
        Message = Session("Message") 
        '
        ' Then decide what to do
        '
        ' We might be here because of "Add" button,
        ' "Remove" button, or just because user hit
        ' "BACK" in the browser.
        '

        If Request.Form("AddFromAvailable") = "Add >>" Then    
            ' user asked to add some value(s)
            For i = 1 To Request.Form("ItemsAvailable").Count
                item = Request.Form("ItemsAvailable")(i)
                If item <> "#" Then
                    Available(item) = "yes" ' now chosen!
                End If
            Next
            '
        End If
        If Request.Form("RemoveFromChosen") = "<< Remove" Then
            ' user asked to remove some previously chosen values
            For i = 1 To Request.Form("ItemsChosen").Count
                item = Request.Form("ItemsChosen")(i)
                If item <> "#" Then
                    Available(item) = "no" ' no longer chosen
                End If
            Next
            '
        End If
        '
        ' save for next time here
        '
        Set Session("Choices") = Available

    End If
        
    ' miscellany:
    iCount = Available.count + 1

%>

<center>

<font Size="+2">
    Dual List Query Demo: Page 1
</font>

<% 
    If Left( Message, 1 ) <> "#" Then
%>
<p>
<font Size="+1"><em>
<strong>Notice:</strong> <% = Message %>
</em></font>
<%
    End If
%>

<p>&nbsp;<p>&nbsp;<p>

<form Name="ModifyLists" Action="listdual.asp" Method="POST">

<table Border="2" CellSpacing="5" CellPadding="5">

    <tr>
        <td>Available Items</td>
        <td>&nbsp;</td>
        <td>Items to Look For</td>
    </tr>
    <tr>
        <td>    
        <select Name="ItemsAvailable" Multiple Size="<% = iCount %>" Width="150">
<%
        aKeys = Available.keys
        For i = 0 To UBound( aKeys )
            item = aKeys( i )
            ' In this demo, we do NOT display already chosen
            ' items in the "available" Select list...but
            ' you could easily eliminate the following
            ' If...Then test and always display all available
            ' choices, if you preferred!
            '
            If Available( item ) = "no" Then
%>
                <option Value="<% = item %>"><%= item %></option>
<%
            End If
        Next
        '
        ' Value="#" option is just to give a minimum
        ' width to the Select list when it would
        ' otherwise be empty:
%>
            <option Value="#">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </option>
        </select>
        </td>

        <td>
            <table Border="0">
            <tr><td>&nbsp;</td></tr>
            <tr><td><input Type="Submit" Name="AddFromAvailable" Value="Add &gt;&gt;"></td></tr>
            <tr><td>&nbsp;</td></tr>
            <tr><td><input Type="Submit" Name="RemoveFromChosen" Value="&lt;&lt; Remove"></td></tr>
            <tr><td>&nbsp;</td></tr>
            </table>
        </td>

        <td>
        <select Name="ItemsChosen" Multiple Size="<% = iCount %>" Width="150">
<%
        chosenCount = 0
        aKeys = Available.keys
        For i = 0 To UBound( aKeys )
            item = aKeys( i )
            '
            ' we only display chosen "yes" items here...
            '
            If Available( item ) = "yes" Then
                chosenCount = chosenCount + 1
%>
                <option Value="<% = item %>"><%= item %></option>
<%
            End If
        Next
%>
            <option Value="#">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </option>
        </select>
        </td>

    </tr>

</table>

</form>

<form Name="ProcessQuery" Action="listdualrespond.asp" Method="POST">
<center>
    <input Type="Submit" Name="Query" Value="Look in DB">
</center>
</form>

<%
    ' Clean up by setting appropriate flags!
    '
    Session("ChosenCount") = chosenCount
    Session("Message") = "#none"
%>

</body>
</html>

Here is the responder script.

   filename=/learn/test/listdualrespond.asp

<Test Script Below>



<%
    '
    ' Protection: You can't come here until
    ' you have set up the session variable
    ' with the list of choices.
    '
    If Session("BeenHere") <> "DoneThat" Then
        Response.Redirect "DualList.asp"
    End If
    '
    ' We don't try to process queries until
    ' we have at least one item chosen...
    ' Naturally, you could change this to 
    ' use some default instead!
    '
    If Session("ChosenCount") = 0  Then
        Session("Message") = "You must place at least one item in the right hand list before asking for a query."
        Response.Redirect "DualList.asp"
    End If
%>

<HTML>
<BODY>

<CENTER>
<FONT Size="+2">
    Dual List Query Demo: Page 2
</FONT>
<P>&nbsp;<P>

This page is a dummy.<br>
It just shows a list of the items chosen<br>
on the prior page.  In a real application<br>
presumably these items would be processed<br>
by some sort of database query.<P>

<P><FONT Size="+1">The List:</FONT><P>
<OL>

<%
    Set Chosen = Session("Choices")
    aKeys = Chosen.keys
    For i = 0 To UBound( aKeys )
        item = aKeys( i )
        If Chosen( item ) = "yes" Then
            Response.Write "<LI>" & item & vbCrLf
        End If
    Next
%>

</OL>

</CENTER>
</BODY>
</HTML>


Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site