HOWTO: Manually Populate a Listbox Design-Time Control at Run Time

ID: Q239577


The information in this article applies to:
  • Microsoft Visual InterDev, version 6.0


SUMMARY

By default, the Listbox Design-Time Control (DTC) is designed to pull data from a data source or have its options statically defined in the Listbox DTC properties. This article describes how to dynamically populate the Listbox at run time.


MORE INFORMATION

Use the following steps to dynamically populate the options of Listbox1 at run time.

  1. Add a Listbox DTC to your page and name it Listbox1.


  2. Add the following Visual Basic Scripting Edition (VBScript) code to your page:
    
    <SCRIPT LANGUAGE=vbscript RUNAT=Server>
    Sub populatelistbox
        Dim mylist(2)
        mylist(0) = "First Option"
        mylist(1) = "Second Option"
        mylist(2) = "Third Option"
    	For i = 0 to UBound(mylist)
    		Listbox1.addItem mylist(i), mylist(i)
    	Next
    End Sub
    </SCRIPT> 
    Alternatively, add the following JScript code to your page:
    
    <SCRIPT LANGUAGE=jscript RUNAT=Server>
    function populatelistbox()
    {
    	var mylist = new Array();
                    mylist[0] = "First Option";
                    mylist[1] = "Second Option";
                    mylist[2] = "Third Option";
    	for (var i = 0;i<mylist.length;i++)
    	{
    		Listbox1.addItem (mylist[i],mylist[i]);
    	}
    }
    </SCRIPT> 


  3. Finally, call the function within the ASP code of your page and before the Listbox DTC:
    <%populatelistbox%> 


Additional query words:

Keywords : kbCtrl kbVisID600 kbGrpASP kbDSupport
Version : WINDOWS:6.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: September 23, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.