ASP.NET Listbox or DropDown Listbox is basically a HTML select control.
We can use Javascript to manipulate the listbox at client side.

Following is the code snippet for doing that

To remove all options from a listbox:

 function clearListBox(listboxID)
 {
 	// returns 1 if all items are sucessfully removed
	var mylistbox = document.getElementById(listboxID);
	if(mylistbox == null)
	  return 1;
	while(mylistbox.length > 0)
	{
	  mylistbox.remove(0);
	}
	  return 1;
 }

To add an options to a listbox:

 function AddOptions(listboxID,text,value)
 {
	var mylistbox = document.getElementById(listboxID);
	var myOption = new Option(text, value);
	mylistbox.options[mylistbox.options.length] = myOption;
 }

Advertisement