You were on the right track with the bit of JavaScript you had. To add items to a listbox through a clientside script you would do something like:
javascript Code:
function addNewOption()
{
var labelText = document.getElementById('Label1').innerText'
var msg= document.getElementById('TextBox3').value;
var optionText = labelText + ' ' + msg;
var optionValue = msg;
var objOption = new Option(optionText, optionValue)
var listObject = document.getElementById('ListBox1');
var itemPosition = listObject.length;
listObject.options[itemPosition] = objOption;
return false;
}
Now there are a few problems with this code. First off you are referencing your Server controls with the value that you are supplying for the ID property. The problem here is that your controls may not be named this on the clientside when they are ultimately emitted and sent to the browser. For example, a textbox in a user control may actually wind up with an ID of: UserControl_TextBox1! To circumvent this problem you should get used to using syntax such as:
var listObject = document.getElementById('<%=ListBox1.ClientID%>');
This will ensure that the proper ID is passed in to your javascript.
Next, since you made the comment about a hidden field, you probably already know that items that are added to the select list in this manner are not accessible in your code behind since they do not exist in viewstate (although you SHOULD be able to access the values that were selected via the POST through a call to Request.Form["Key"]). In any event, if you want to append the values to a hidden field you would use a javascript similar to the following:
javascript Code:
function selectOnChange()
{
var textBox = document.getElementById('textHide');
var listObject = document.getElementById('test');
textBox.value = '';
for(i=0; i <= listObject.options.length - 1; i++)
{
if(listObject.options[i].selected)
textBox.value = textBox.value + listObject.options[i].value + ', ' ;
}
}
**Notes
-I tested all code in IE 7.0 without a problem.
-Becareful with the <% %> script blocks inside of your javascripts. This can cause errors to be thrown in the manner of 'Can not change the control collection because the code contains <% %>'. By and large I see this error when there is an AJAX framework at play and various user controls on a page.
hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library:
Wrox Books 24 x 7
Did someone here help you? Click

on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================