ListBox items changed client-side are not available server-side

V

Valerian John

I have a ListBox webcontrol on an aspx page. Items are added to the
ListBox using client-side code. However, when the page is posted back the
items are missing/not available. (It is like the page does not know the
items were added client-side.)

TIA,
Val
 
A

Ashish M Bhonkiya

Hi Valerian John,

Returning values from a client side encoded page is
entirely dependent on your client side code. I assume when
you say "client side", you are talking about JavaScript?
Is your listbox view state enabled? Does it carry out auto
postback? There are various strategies available to post
back client side added data. I use a very basic method,
which involves naming my control (in your case a list
box), and setting it's values equal to your client added
values. On the server side, you will simply have to pull
the values out of the request string and do with them
whatever you want to do.

Regards
Ashish M Bhonkiya
 
E

Eliyahu Goldin

The ViewState of a webcontrol is a collection of the control's properties.
When you add items on client-side, you are using options collection of a
select object, aren't you? Probably it doesn't translate to ListBox Items
property and therefore doesn't get posted back.

Eliyahu
 
V

Valerian John

Ashish:

Thanks for your response.
Here is the basic JavaScript function I am using for adding an OPTION
element to the SELECT created by the ListBox webcontrol. The item/option is
added to the ListBox but after postback when I list the items in the ListBox
this newly added item is missing.

Button1 is a HTML button & Button2 is Button webcontrol. Button1's onclick
attribute is set to the client-side addOption() function. Button2 does the
postback and calls the server-side GetItems() proc. TextBox1 is a
multi-line Textbox webcontrol used to display all the ListBox items. The
ListBox's AutoPostBack property is set to false & its EnableViewState is
true.

function addOption() {
var oListBox=document.getElementById('ListBox1');
var oOption = document.createElement("OPTION");
oOption.text="Test";
oOption.value="Test";
oListBox.add(oOption);
return true;
}

Private Sub GetItems()
Dim i As Integer=0
While (i < ListBox1.Items.Count)
TextBox1 .Text += ListBox1.Items(i).Text + vbNewLine
i += 1
End While
TextBox1 .Text += ListBox1.Items.Count.ToString + vbNewLine
End Sub

Appreciate your input/help,
Val
 
V

Valerian John

Eliyahu:

Thanks for your response.
Yes, I am using the Options collection of the Select element.
The ListBox webcontrol is rendered as an HTML Select so I figured I should
be able to manipulate it client-side.
I am wondering if one must call a built-in client-side function (similar to
__doPostBack) that registers that the ListBox's items were altered and that
changes must be passed via ViewState on postback.

TIA,
Val
 
V

Valerian John

Eliyahu:

Thanks for that url. I am testing out his DynamicListBox for this problem.
Found some other useful controls there.

Val
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top