Reputation: 6405
I populate a collection in a UserControl
which then creates dynamic LinkButton
controls from the collection. When one is clicked, on postback, the collection is empty. Is this behavior by design or is there something I can do to keep the collections populated on postback without re-populating them in code? Do I need to re-query in the forms postback?
Upvotes: 0
Views: 212
Reputation: 21485
You need to persist the information in ViewState
and recreate the controls when the ViewState is loaded after postback. For example, you could store number of buttons created and then recreate them.
See a similar solution at https://stackoverflow.com/a/15497035/1711598
Upvotes: 1
Reputation: 460058
Every variable defined in a class inheriting Web.UI.Page
will be destroyed at the end of the Page-Lifecycle(incl. controls and fields), hence it will be null
at postback if you don't reinitialize it.
One way to persist it across postbacks is to store it in a Session-variable.
You will find a complete list of all options on how to persist variables across postbacks here: http://msdn.microsoft.com/en-us/magazine/cc300437.aspx
It's the nature of HTTP that it is stateless.
Upvotes: 2