views:

832

answers:

2

Hi All, in ASP.NET, I've bound a listbox control to a List of custom class (consisting of a TimeSpan and Int members). the ToString() function just displays them both as a long string.

Now I want to save the modified listbox into the DB again, I need to use the custom class objects again.

Does the ListBox save the actual custom objects once its databound? or only the string representations?

If so, how do I get them from the LB?

+1  A: 

Googling doesn't seem to help much, but it seems to me that a databound ListBox contains objects called ListItems, which have a Text and a Value property. The databinding figures out which is which from your custom class and displays them.

Short answer: no, it does not look like the ListBox is storing the actual class.

Matthew Jones
Yeah it seems like that's the case.. Bummer. Do you have an alternative to offer me in this respect?
Roey
When you need to save the data, you could iterate through the text box, create the class you need from the Text and Value, and save that class (i.e. overwrite the class that combination originally came from). Does that do what you are asking?
Matthew Jones
However, my suggestion assumes that you can uniquely identify your date using the Text and/or Value parameters. Do you have a single unique identifier? If you do, bind it to the Value field to make your life easier.
Matthew Jones
+1  A: 

Once you've created the values into a collection which you are binding to your listbox then you could use viewstate to persist them between postbacks.

e.g.

ViewState["TimespanItems"] = myListHere;

myListHere = (myListTypeHere) ViewState["TimespanItems"];

Peter