467,075 Members | 982 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

adding items to <asp:ListBox> via javascript

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
Nov 19 '05 #1
  • viewed: 2490
Share:
3 Replies
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.
"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:uO**************@TK2MSFTNGP14.phx.gbl...
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

Nov 19 '05 #2
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
Nov 19 '05 #3
Yes, this is possible...not using standard techniques though.

See
http://www.dart.com/livetutorial/dis...ist%20Controls

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Kevin Blount | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.