adding items to <asp:ListBox> via javascript

R

Ryan Taylor

Hello.

I have an application where I need the user to be able to add items to a
listbox. I've implemented this via javascript. The listbox is an
<asp:ListBox>. However, when the user submits the form those items do not
get posted. My listbox.Items.Count always returns 0.

I want the user to be able to add items without having to resort to a
postback, hence the javascript. But I need to be able to access those items
in the code behind page as well. Any thoughts on how this is best achieved?

Thanks in advance.
Ryan Taylor
 
M

Marina

I don't think that can happen. When you populate a listbox on the server,
all that stuff is stored via viewstate (which is how the listbox stays
populated after a postback). Since you can't hack the viewstate, I don't see
how you could keep the listbox populated, other then passing the items that
should go in it as a list in a hidden input or something, and manually
populating on the server.
 
R

Ryan Taylor

That is really unfortunate. To put the application into context, the
application is allowing a logged in user to add new employees. The listbox
is a list of degrees that the employee may have. You can create new
employees, edit employees and delete employees. As such, the degrees listbox
needs to be able to insert/remove degrees. I really wanted to avoid using a
postback because it looks unprofessional, it should appear that the form is
only posted once. Also, and this is the worst part, when the page does a
postback after adding an item to the listbox, the page resets so we are now
at the top of the page. Since this is a very long form, the user is forced
to scroll back down to the bottom of the page.

If there was at least a solution to resume in the same location as before
the postback, then that might be acceptable.
Thanks for replying so quickly.

Ryan Taylor
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top