Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on ListBox Control

Status
Not open for further replies.

VentureNewBie

Programmer
Jul 17, 2001
4
IN
Hi!

This is the piece of code.

I would like to know how I can remove all the items from ListBox1 and populate the same items in ListBox2 when the user clicks on a button named "SelectAll"

<%@ Language=VBScript %>
<HTML>
<HEAD>
<LINK REL=&quot;stylesheet&quot; TYPE=&quot;text/css&quot; HREF=&quot;../tms.css&quot;>
<title>Trial Menu Page</title>
</HEAD>
<form name=TrialMenuPage method=post>
<BODY>

<%
Set oConn = Server.CreateObject(&quot;ADODB.Connection&quot;)

'This line creates an ADO Connection Object named oConn.
'To use the oConn ADO connection we must first open it:

oConn.Open(&quot;provider=SQLOLEDB.1;UserId=tmsuser;password=tms;Initial Catalog=TMS;data source=testing-tms;&quot;)

set oRs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
oRs.ActiveConnection = oConn
oRs.CursorType = 1
oRs.LockType = 1
oRs.Open &quot;SELECT BankID, BankShortName from BankMaster order by BankShortName&quot;

%>
<SCRIPT LANGUAGE=javascript>
<!--

/* Add groups selected from lstSelected list box to lstAvailable list box */

function Select_All()
{
/* Find selected items to add */
var cSelect = document.all.item(&quot;lstSelected&quot;); /* Listbox1 */
var cOut = document.all.item(&quot;lstAvailable&quot;); /* Listbox2 */

for (var i=0; i < cSelect.options.length; i++)
{
/* Check to see if a particular option is selected */
if(cSelect.options(i).selected)
{
/* Add new option to &quot;Groups Selected&quot; list box */
cOut.options[cOut.options.length]= new Option(cSelect.options(i).text, &quot;value &quot; + i);
cSelect.remove(i)
}
}
}


-->
</SCRIPT>


<center><h3>Trial Menu Page</h3></center>
<TABLE align=&quot;center&quot; border=0 cellPadding=1 cellSpacing=1 style=&quot;HEIGHT: 208px; WIDTH: 444px&quot;
width=&quot;75%&quot;>

<TR>
<TD><SELECT multiple id=lstAvailable name=lstAvailable size=2 style=&quot;HEIGHT: 204px; WIDTH: 186px&quot;>

</SELECT></TD>

<TD>
<P><INPUT id=btnSelectAll name=btnSelectAll type=button value=&quot;>>&quot; style=&quot;HEIGHT: 24px; WIDTH: 57px&quot; onclick=&quot;Select_All()&quot;></P>
<P><INPUT id=btnSelectOne name=btnSelectOne type=button value=&quot;>&quot; style=&quot;HEIGHT: 24px; WIDTH: 57px&quot; onclick=&quot;Select_One()&quot;></P>
<P><INPUT id=btnDeSelectAll name=btnDeSelectAll type=button value=&quot;<&quot; style=&quot;HEIGHT: 24px; WIDTH: 55px&quot; onclick=&quot;Deselect_One()&quot;></P>
<P><INPUT id=btnDeSelectOne name=btnDeSelectOne type=button value=&quot;<<&quot; style=&quot;HEIGHT: 24px; WIDTH: 57px&quot; onclick=&quot;Deselect_All()&quot;><br></P>
<CENTER> </CENTER>
</TD>
<TD><SELECT id=&quot;lstSelected&quot; name=&quot;lstSelected&quot; size=10 style=&quot;HEIGHT: 204px; WIDTH: 186px&quot; onclick=&quot;Select_All()&quot;>

<%while Not oRs.EOF%>

<OPTION value=&quot;<%=oRs.Fields(0).Value%>&quot; selected> <%=oRs.Fields(1).Value%></OPTION>

<%oRs.MoveNext()
wend
%>

</SELECT>
</TD>
</TR>
</TABLE>

<INPUT id=btnNext name=btnNext type=button value=Next style=&quot;HEIGHT: 24px; WIDTH: 54px&quot;> 
<INPUT id=btnFinish name=btnFinish type=button value=Finish>
</P>
<input type=&quot;hidden&quot; name=&quot;SelectFlag&quot;>
</BODY>
</form>
</HTML>
 
VentureNewbie,

Try something like the following. I have used iza's FAq to generate this code. You will have to substitute your own form and select names into this code.

function select_all()
{
var listbox1 = document.myForm.mySelect1
var listbox2 = document.myForm.mySelect2
var oOption;

// Add all the options to the second listbox
for (var i = 0; i < listbox1.options.length; i++) {
oOption = document.createElement(&quot;OPTION&quot;);
oOption.text = listbox1.options.text;
oOption.value = listbox1.options.value;
listbox2.options.add(oOption);
}

// remove all the options from listbox2
while (listbox1.options.length != 0)
listbox1.options.remove(0);
}

I haven't tested this out but I hope that it should work. Mise Le Meas,

Mighty :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top