Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Two ListBoxes dynamically populated w/JavaScript

Two ListBoxes dynamically populated w/JavaScript

Two ListBoxes dynamically populated w/JavaScript

I'm an asp.net newbie.
I have 2 listboxes (lbCouponList,lbCouponSelected).
The second listbox (lbCouponSelected) is populated using Javascript from the items selected in the first listbox (lbCouponList).
When the form is posted back, the lbCouponSelected listbox has none of the items inserted by the javascript client.
* I don't need to re-populate the listbox, I only need to read the items in the lbCouponSelected.*
I found out that if the listbox is pre-populated in the server, I'm able to read the listbox items pre-populated, but not the new items added by the client javascript code.
Any idea how can I read the dynamically populated listbox ?


RE: Two ListBoxes dynamically populated w/JavaScript

The changes you make to a list box through client javascript live on the client and the server doesn't see them. Before you post the form back to the server, you need to save the added items into a control that both client and server code can see. A very good candidate for this is the HtmlInputHidden control.

What you would do is write a javascript function that saves the added items into the value property of the HtmlInputHidden control. This function would run right before the postback. Now, on the server, you would read the value property of the HtmlInputHidden control and get the added items.

The javascript function I use looks like this:

// This function runs just before the postback
function SaveAddedItems(hiddenControl)
   for (i = 0; i < lst.length; i++)
      added += ";" + lst.options[ i ].value;
   hiddenControl.value = added;

Then, on the server I have a function that works with the selected items. It looks like this:

// C# code on the server parses added items.
private void WorkWithAddedItems()
   string added = hiddenControl.value;
   string addedArr[] = added.Split(';');
   // ...
   // Do whatever I want with the items.

Notice this: On the javascript function, I use a semicolon, ";", to separated each added item but you could use whatever is suitable to you. Also, I use the value property of the list item, as opposed to its text property, but again, you should do whatever is suitable to you.


We don't see things how they are; we see them how we are.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close