Solved

add items to a listbox

Posted on 2001-06-28
15
997 Views
Last Modified: 2012-06-21
How to use VBscript add items to an empty listbox when load the web page?
0
Comment
Question by:jyx
  • 4
  • 4
  • 2
  • +3
15 Comments
 
LVL 7

Expert Comment

by:daniel_c
ID: 6235782
Try this:

<HTML>
<BODY ONLOAD="document.form1.sel1.length=0">
<FORM NAME=form1>
<select name=sel1>
                                   <option value=1>1
                                   <option value=1>2
</select>
</FORM>
</BODY>
</HTML>
0
 
LVL 1

Expert Comment

by:dclary
ID: 6235827
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("Select 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>
0
 
LVL 11

Expert Comment

by:Otana
ID: 6235836
for dynamic loading, put

<% Do While Not RS.EOF %>
    <option value='<%= RS.Fields("FieldName1").Value %>'>
        <%= RS.Fields("FieldName2").Value %>
    </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.
0
 
LVL 11

Expert Comment

by:Otana
ID: 6235848
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.
0
 
LVL 7

Expert Comment

by:daniel_c
ID: 6235852
ooopsss... i think i misunderstood the question... :P
0
 

Author Comment

by:jyx
ID: 6236016
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.GetAllPrinters,"~")

for x=0 to ubound(printername)
document.form1.list2.length =  document.form1.list2.length +l
document.form1.list2(x).value= printername(x)
next

end sub

</script>

0
 
LVL 7

Expert Comment

by:daniel_c
ID: 6236117
try this example (hope I won't misunderstand again this time):


<html>
<head><title></title></head>
<script language="javascript">
<!--
function Pop(){
var FullName = document.form.Title[document.form.Title.selectedIndex].value;
     FullName += ", ";
     FullName += document.form.surname.value;
     alert(document.form.AddedList.length);
     document.form.AddedList.options[document.form.AddedList.length] = new Option(FullName,FullName);
     document.form.AddedList.selectedIndex = 0;
     document.form.AddedList.reset;
}

function rem(){
     document.form.AddedList.remove(document.form.AddedList.selectedIndex);
     document.form.AddedList.reset;
}
//-->
</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,

^_^
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:dclary
ID: 6236157
Bah, html! ASP is the way to be! Buwahaha!

=)


0
 
LVL 7

Expert Comment

by:daniel_c
ID: 6236181
dclary,read jyx's last comment:
"I only want to use HTML, ..."
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 50 total points
ID: 6236393
Try this

Use Span tag

on your form
<span id="Span1">
</span>

<script language = vbscript>

Sub window_OnLoad()
dim printername
dim x, str
printername = split(SystemInfo.GetAllPrinters,"~")

str = "<SELECT MULTIPLE size=5 id=List2 name=List2 style='FLOAT: right; width:100%'>"

for x=0 to ubound(printername)
Str = str & "<option>" & printername(x) & "</option>"
next

str = str & "</SELECT>"

span1.innerhtml = str
end sub

</script>
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 6236779
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.PeriodForm.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.length - 1;i++)
          {
               var optionValue = ArrPeriod[i]
               var newOptionName = new Option(optionValue);
               document.PeriodForm.START.options[document.PeriodForm.START.length] = newOptionName;
          }
}
</script>
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 6236782
call this function in body onload or wherever you like and you can get your list.bingo!
0
 

Author Comment

by:jyx
ID: 6238151
I appreciate all your guys' help, they are all valuable. But EDDYKT's answer is exactly what I want!
0
 

Author Comment

by:jyx
ID: 6238334
But another problem, how to retrieve the value which I selected in that listbox?
thanks
0
 

Author Comment

by:jyx
ID: 6238742
Nothing is wrong now. I retieved the selected value.
thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Round to 2 decimal places 2 40
currencylayer API intergrate to ASP 25 66
how to hide hide popup page refresh second time? 10 112
Need help editing script 3 66
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now