0

After wasting hours for trying to solve dynamic user control's ViewState being lost I decided to disable ViewState for good.

The question is what should I do now? How should I keep my dynamic control's state so that they don't get lost after postbacks. I am thinking about using Session instead but that means I have to generate unique keys for each page/tab opened by the user so that values are not overwritten (right?). What is the best way of doing it?

1
  • I ended up disabling ViewState and using HiddenFields instead.
    – emregon
    Jan 14, 2013 at 8:47

1 Answer 1

0

For ensuring view-state of dynamic user controls, you need to ensure that

  • Dynamic controls are created in every post-back scenario
  • They are created as early as possible in page life cycle - init and load state are best bets (loading them in control events is unlikely to work)
  • The dynamic control hierarchy should be exactly the same and all controls in hierarchy should have ids same as in previous request
  • Always assign ids (otherwise they may get auto-generated and can have different values) and assignment should happen before adding the control in control tree.

Changing the view-state store to say session instead of hidden field will not solve the problems where ASP.NET run-time has already loaded the view-state or is unable to associate control with its view-state data (inferred from id and hierarchy)

2
  • I can verify that the controls are getting the same UniqueIDs after each postback. Is it possible that loading two dynamic controls of the same type cause problems? Although they have different uniqueID's they are having the same server side IDs.
    – emregon
    Jan 10, 2013 at 11:16
  • @Kta, as long as naming containers are different (i.e. diff unique ids), it should not be an issue. Make sure that id gets assigned before control is added to the parent. You can override LoadControlState/SaveControlState to inspect view-state bag!
    – VinayC
    Jan 10, 2013 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.