|
Dual List Boxes by Bill Wilkinson
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
<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> <p> <p>
<form Name="ModifyLists" Action="listdual.asp" Method="POST">
<table Border="2" CellSpacing="5" CellPadding="5">
<tr>
<td>Available Items</td>
<td> </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="#">
</option>
</select>
</td>
<td>
<table Border="0">
<tr><td> </td></tr>
<tr><td><input Type="Submit" Name="AddFromAvailable" Value="Add >>"></td></tr>
<tr><td> </td></tr>
<tr><td><input Type="Submit" Name="RemoveFromChosen" Value="<< Remove"></td></tr>
<tr><td> </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="#">
</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
<%
'
' 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> <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>
 |  |  |
 |
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too.
a $5 tip buys him lunch at McDonalds,
a $20 tip buys his kid Hitoshi a new computer game,
a $39 tip buys his daughter Michiko a few nice outfits.
See our donor list.
|  |
 |  |  |
|
|
|
|