M. Carlo Bramini
M. Carlo Bramini

Reputation: 2522

class object serializable for ViewState storage not working in c# ASP.net

I’ve created a panel to store my ListBoxes, my goal is to add new Listboxes dynamically when I press the Button control on the page.

I’ve created a MyList class that inherits List class and I’ve applied the [Serializable] tag. I've applied serialized to store the object in the viewstate.

The MyList class will store all the ListBoxes dynamically created.

questions:

page laoded before clicking:

enter image description here

error message after clicking button: enter image description here

Upvotes: 3

Views: 3250

Answers (1)

Brian MacKay
Brian MacKay

Reputation: 32037

Although you've marked your class [serializable], it depends on ListBox which is not serializable.

I did a little googling and found this which may help, although it looks more involved than I would expect.

Couple quick thoughts:

  • Dynamic content is one of the weaker spots in WebForms because, as you're seeing, the dynamic content is lost after postback, so it must be persisted and re-created in some way. In these situations, I try to and eliminate postbacks and rely more on JavaScript so the browser never gets reset... And this is one of the places where the new MVC model shines, because it isn't constantly fighting you (as the postpack model inadvertently does). Actually, this pain is one of the main things nudging us towards single page applications.

  • You might be better off writing your own class that contains what you care about regarding these listboxes, such as position and their contents which is hopefully just some simple strings, and serialize that instead... Then write your own code to de-serialize them into ListBoxes.

Nothing against webforms by the way, I have one open right now in another window!

Upvotes: 2

Related Questions