Listbox item added with client scripts not submitting with ASP:Listbox

S

Simon Prince

Help

I have a ASP:Listbox on a form. My page Adds items to this this via
Client-Side Script only.
Such as...

var vObj_TargetElement =
document.getElementById("_PageTemplate_innerHolder_oASP_Lbx_Invoice");
var vObj_SourceElement = document.getElementById("oHTML_Tbx_Invoice");
var vInt_OptionTotal = vObj_TargetElement.length++;
vObj_TargetElement.options[vInt_OptionTotal].value =
Trim(vObj_SourceElement.value);
vObj_TargetElement.options[vInt_OptionTotal].text =
Trim(vObj_SourceElement.value);


When the form is submitted, these new items are not submitted within the
LISTBOX.

Server-Side code such as ...
Response.Write(oASP_Lbx_Invoice.Items.Count.ToString)

Returns 0 (zero)

Any ideas how to solve this ??

Thanks.

Simon

[Remove the NOSPAM if replying by email]
 
J

jjardine

Simon Prince said:
Help

I have a ASP:Listbox on a form. My page Adds items to this this via
Client-Side Script only.
Such as...

var vObj_TargetElement =
document.getElementById("_PageTemplate_innerHolder_oASP_Lbx_Invoice");
var vObj_SourceElement = document.getElementById("oHTML_Tbx_Invoice");
var vInt_OptionTotal = vObj_TargetElement.length++;
vObj_TargetElement.options[vInt_OptionTotal].value =
Trim(vObj_SourceElement.value);
vObj_TargetElement.options[vInt_OptionTotal].text =
Trim(vObj_SourceElement.value);


When the form is submitted, these new items are not submitted within the
LISTBOX.

Server-Side code such as ...
Response.Write(oASP_Lbx_Invoice.Items.Count.ToString)

Returns 0 (zero)

Any ideas how to solve this ??

Thanks.

Simon

[Remove the NOSPAM if replying by email]

It looks as though a listbox is read only as far as once it goes down to the
client as html. The server control does not look at what happens to the
list box once it leaves the server. When you go back to the server it uses
the server version to reload the box. I messed around with the code you
posted and that seems to be what is happening. It seems to be a one way
street. Is there any way to have the listbox do a postback to update it?
 
G

Guest

The way it is normally done is through a hidden field. Whenever your
client-side script runs to add the value to the list, add it to the hidden
field as well. On postback repopulate the listbox with the values from the
hidden field. You need to be careful because, if I remember correctly, the
listbox will retain whatever values were in it when it was originally sent to
the client. So either handle this case or always have the hidden field
represent values that were added during the current client session (prior to
postback).

I have seen a custom control somewhere that did just what you wanted but I
can't remember where. You could try Code Project or Code Guru.

Michael Taylor, MCP - 10/19/04
 
Top