Thursday, March 22, 2012

Listbox and Postback help needed

I have a webform with a listbox webcontrol. I am populating this list via client-side code and it works great, however, the only way I can read the contents of what has been placed in this list (that I know of) in the code behind is using request.form("listname").Split(",") and iterate through an array. In order to do even that, I have to select all items in this listbox via client side code whenever a postback occurs, and I have about 3 or so controls that can cause a postback. In order to even write the client-side code to select the items in this list, I have to try and alter dopostback. There has got to be an easier way. Is it possible to read the items in a listbox if they are not selected, through anther method of the request object, or an easier way to integrate client-side code into doPostback? Any help would be appreciated.is the listbox set to runat="server"? If so you can get anytype of info from using listboxname.items() collection or listboxname.selectedItem property. It is a listbox object that has many properties, events, and methods.
If an item is not selected, only way it can be maintained across postbacks is with the help of viewstate. Since you are adding these items on the clientside, they are not part of the viewstate when you postback, so the server has no idea about this., so you should send them some how to the server

And if the item is not selected, it wouldnt be part of Form posted data. So, in your case there is no simpler way unfortunately.

May be you should consider using some hidden input control and write these items as a string to this hidden input control and send back to server and iterate through this string to retrieve the values. Once the values go back to the server, they would be part of the viewstate, if its enabled for the listbox, right before the page is show to you. So, postbacks from then onwards would maintain these values even if they are not selected
As has been mentioned, you'll have to store these values in a hidden form field or somewhere for retrieval on postback when you can insert them into the list's Items collection.

Some links you may find helpful (these are pre-fab solutions):

DynamicListBox
DualList
EasyListBox

Cheers,
Thanks for the ideas. I think I will go with the hidden field.

0 comments:

Post a Comment