We help IT Professionals succeed at work.

How to access dynamically added listbox item/value on postback

ggd4865 asked
Last Modified: 2012-06-21

I have a listbox.  Items are added to the listbox on the client side via javascript.

Before the form is submitted, all the listbox items are selected.

What is the best way to access the dynamically added items on the server side?

I have no problem accessing items that were previously added on the server side.  They show up as ListIems in the SelectedIndexChanged event.  The dynamically added items do not.

One way I have found is to loop through a NameValueCollection searching for the appropriate key value, e.g. "Box1", and then get the values.

For example, in Page_Load:

NameValueCollection coll1 = Request.Form;
String[] arr1 = coll1.AllKeys;

for (loop1 = 0; loop1<arr1.Length; loop1++)
    if (arr1[loop1] == "box1")  //is this the key I'm looking for?

        String[] arr2 = coll1.GetValues(arr1[loop1]);        // Get all values under this key.

        for (loop2 = 0; loop2 < arr2.Length; loop2++)

This works, but isn't there something more straightforward?  Is there anyway to access Box1 items more directly?

I am new to ASP.NET and have no idea what I am doing.  I hope this means easy points for someone.

Thank you!

Watch Question

To access dynamically items in the listbox:
a) add a listbox called ListBox1 and change selection mode to Multiple
b) create 3 command buttons called Button1, Button2, Button3 and one text box TextBox1
//Button1 will add text in TextBox1 to ListBox1
private void Button1_Click(object sender, System.EventArgs e)
      {      ListBox1.Items.Add(TextBox1.Text);
            TextBox1.Text="";            }
//Button2 will select all items in ListBox1
private void Button2_Click(object sender, System.EventArgs e)
            for (int i=0;i<ListBox1.Items.Count;i++)
            {      ListBox1.Items[i].Selected=true;}
//Button3 will list last item added
private void Button3_Click(object sender, System.EventArgs e)
      {TextBox1.Text = ListBox1.Items[ListBox1.Items.Count-1].Value;      }



Thank you for your answer, but I don't think my question was clear.

On a form, I have two listboxes.  Listbox1's items are added server-side.  The user selects items in Listbox1 and pushes a button to move the item to Listbox2.  The button fires some javascript that moves the item to Listbox2. This occurs on the browser, without going back to the server.  When the form is submitted I need access to the items in Listbox2.  This is pre-existing javascript code, not ASP.Net generated code and is something I need to work with.  I don't have the option of using code like yours that process the move on the server side.


//If Listbox2 is HTML select box, you can reference it using,
protected System.Web.UI.HtmlControls.HtmlSelect ListBox2;

//the rest is same as List Box in normal Web Form control, even if items are transfer from Listbox1 to Listbox2, you can still
//select all the items or dynamically control any one of them.
private void Button1_Click(object sender, System.EventArgs e)
      for (int i=0;i<Listbox2.Items.Count;i++)
                  {     Listbox2.Items[i].Selected=true;}



Unfortunately, it doesn't work.  It seems that, since the javascript is manipulating the listbox items, the changes to the listbox are completely unknown to the server.  When running the app with your code, after I move some items using the javascript and press Button1, the page comes back to the client with all of the listbox items in their original position.  I think that is because the server has know way of knowing that items have beem moved between listboxes.  

Ultimately, this is a little off topic.  My question is:  is there an easier way to access values being posted back to the server in the request stream?  Easier than the, to my eyes, long-winded code posted in my original question.  

In ASP I think it is

str = request("objectName")

or something similar.  I'm not an ASP programmer by trade.

This one is on us!
(Get your first solution completely free - no credit card required)


OK.  I'm happy that was embarrassingly easy.

Re changing the control to a WebForm control--I would have if I could have...

Thank you, bsdotnet.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.