How To Manually Populate a Listbox Design-Time Control at Run Time (239577)



The information in this article applies to:

  • Microsoft Visual InterDev 6.0

This article was previously published under Q239577

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%>

Modification Type:MinorLast Reviewed:7/2/2004
Keywords:kbCtrl kbhowto KB239577 kbAudDeveloper