jyx
asked on
add items to a listbox
How to use VBscript add items to an empty listbox when load the web page?
If you've got a table you want to populate a listbox with you could use. Now, normally I'd encapsulate the code into a function and keep it at the front of the asp doc, but it's up to you.. this is the quick and dirty way...(Code assumes you know how to set up and open recordsets already)
<Select Name=sel1>
<%
response.write vbcrlf
set adors = adocon.openrecordset("Sele ct listboxid, listboxtext from listtable" , 0, 1)
do until adors.eof
response.write "<OPTION value=" & adors.fields(0).value & ">" & adors.fields(1).value & "</OPTION>"
adors.movenext
loop
response.write vbcrlf
%>
</select>
<Select Name=sel1>
<%
response.write vbcrlf
set adors = adocon.openrecordset("Sele
do until adors.eof
response.write "<OPTION value=" & adors.fields(0).value & ">" & adors.fields(1).value & "</OPTION>"
adors.movenext
loop
response.write vbcrlf
%>
</select>
for dynamic loading, put
<% Do While Not RS.EOF %>
<option value='<%= RS.Fields("FieldName1").Va lue %>'>
<%= RS.Fields("FieldName2").Va lue %>
</option>
<% RS.MoveNext
Loop %>
in case you use a recordset. make sure to use the closing tags </option>, it will save you a lot of potential trouble.
<% Do While Not RS.EOF %>
<option value='<%= RS.Fields("FieldName1").Va
<%= RS.Fields("FieldName2").Va
</option>
<% RS.MoveNext
Loop %>
in case you use a recordset. make sure to use the closing tags </option>, it will save you a lot of potential trouble.
and also use the single quotes for value, because when your field value consists of two parts with a space in between,, the value will only contain the first part.
ooopsss... i think i misunderstood the question... :P
ASKER
Thanks.
I only want to use HTML, I mean only use client side VBSCRIPT (NO ASP, NO "response.write", no "<% %>"). It's my fault, maybe I should put this question under HTML subject.
I used an OCX to get the printer setting information of the client computer and put these information into an Array, then I want to show it in the listbox using VBScript.
The following is my code, but it always give me error "object required: document.form1.list2(...)" . list2 is the name of my listbox.
<script language = vbscript>
Sub window_OnLoad()
dim printername
dim x
printername = split(SystemInfo.GetAllPri nters,"~")
for x=0 to ubound(printername)
document.form1.list2.lengt h = document.form1.list2.lengt h +l
document.form1.list2(x).va lue= printername(x)
next
end sub
</script>
I only want to use HTML, I mean only use client side VBSCRIPT (NO ASP, NO "response.write", no "<% %>"). It's my fault, maybe I should put this question under HTML subject.
I used an OCX to get the printer setting information of the client computer and put these information into an Array, then I want to show it in the listbox using VBScript.
The following is my code, but it always give me error "object required: document.form1.list2(...)"
<script language = vbscript>
Sub window_OnLoad()
dim printername
dim x
printername = split(SystemInfo.GetAllPri
for x=0 to ubound(printername)
document.form1.list2.lengt
document.form1.list2(x).va
next
end sub
</script>
try this example (hope I won't misunderstand again this time):
<html>
<head><title></title></hea d>
<script language="javascript">
<!--
function Pop(){
var FullName = document.form.Title[docume nt.form.Ti tle.select edIndex].v alue;
FullName += ", ";
FullName += document.form.surname.valu e;
alert(document.form.AddedL ist.length );
document.form.AddedList.op tions[docu ment.form. AddedList. length] = new Option(FullName,FullName);
document.form.AddedList.se lectedInde x = 0;
document.form.AddedList.re set;
}
function rem(){
document.form.AddedList.re move(docum ent.form.A ddedList.s electedInd ex);
document.form.AddedList.re set;
}
//-->
</script>
<body>
<form name="form">
<select name="Title">
<option value="Mr">Mr
<option value="Miss">Miss
<option value="Mrs">Mrs
</select>
<input type="text" name="surname">
<select name="AddedList">
<option value="None">--- Select One ---
</select>
<input type="button" value="Add" onclick="Pop()">
<input type="button" value="Remove" onclick="rem()">
</form>
</body>
</html>
Cheers,
^_^
<html>
<head><title></title></hea
<script language="javascript">
<!--
function Pop(){
var FullName = document.form.Title[docume
FullName += ", ";
FullName += document.form.surname.valu
alert(document.form.AddedL
document.form.AddedList.op
document.form.AddedList.se
document.form.AddedList.re
}
function rem(){
document.form.AddedList.re
document.form.AddedList.re
}
//-->
</script>
<body>
<form name="form">
<select name="Title">
<option value="Mr">Mr
<option value="Miss">Miss
<option value="Mrs">Mrs
</select>
<input type="text" name="surname">
<select name="AddedList">
<option value="None">--- Select One ---
</select>
<input type="button" value="Add" onclick="Pop()">
<input type="button" value="Remove" onclick="rem()">
</form>
</body>
</html>
Cheers,
^_^
Bah, html! ASP is the way to be! Buwahaha!
=)
=)
dclary,read jyx's last comment:
"I only want to use HTML, ..."
"I only want to use HTML, ..."
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i think a simple javascript function can serve the purpose.
i'm pasting some of my code here.
hope it helps...
here we go...
<script>
function PopList()
{
var i;
for(i=0;i<document.PeriodF orm.START. length;i++ )
{
{
document.PeriodForm.START. options[i] = null;
i--
}
}
var ArrPeriod = new Array
ArrPeriod[0] = "Select Week"
for(i=1;i<=52;i++)
{
ArrPeriod[i] = "Week" + i
}
for(i=0;i<=ArrPeriod.lengt h - 1;i++)
{
var optionValue = ArrPeriod[i]
var newOptionName = new Option(optionValue);
document.PeriodForm.START. options[do cument.Per iodForm.ST ART.length ] = newOptionName;
}
}
</script>
i'm pasting some of my code here.
hope it helps...
here we go...
<script>
function PopList()
{
var i;
for(i=0;i<document.PeriodF
{
{
document.PeriodForm.START.
i--
}
}
var ArrPeriod = new Array
ArrPeriod[0] = "Select Week"
for(i=1;i<=52;i++)
{
ArrPeriod[i] = "Week" + i
}
for(i=0;i<=ArrPeriod.lengt
{
var optionValue = ArrPeriod[i]
var newOptionName = new Option(optionValue);
document.PeriodForm.START.
}
}
</script>
call this function in body onload or wherever you like and you can get your list.bingo!
ASKER
I appreciate all your guys' help, they are all valuable. But EDDYKT's answer is exactly what I want!
ASKER
But another problem, how to retrieve the value which I selected in that listbox?
thanks
thanks
ASKER
Nothing is wrong now. I retieved the selected value.
thanks.
thanks.
<HTML>
<BODY ONLOAD="document.form1.sel
<FORM NAME=form1>
<select name=sel1>
<option value=1>1
<option value=1>2
</select>
</FORM>
</BODY>
</HTML>