troubleshooting Question

How to access dynamically added listbox item/value on postback

Avatar of ggd4865
ggd4865 asked on
ASP.NET
6 Comments1 Solution670 ViewsLast Modified:
All,

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++)
        {
            doSomeThingWithValue(arr2[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!

--ggd4865
ASKER CERTIFIED SOLUTION
bsdotnet

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 6 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 6 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004