views:

101

answers:

1

Have a page that adds controls dynamically. Control state is being retrieved from database and controls are re-added to page on postback. User can add/delete controls through UI. When two users are on the same page and one adds/removes controls the other user will get ViewState error next time he tries to modify control state.

"Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request."

This is because when page is drawn I use one set of controls and I get a different set of controls on postback because control state was modified by different user.

I understand the error and why its happening but I think I'm missing something here. Is there a way to have a page that dynamically adds controls and can handle concurrent users that modifiy the state of controls?

+1  A: 

You need to separate the current user's state from the master, or global, state. On postback, instead of building your control tree based on the master definition in the database, load a cached control tree that is specific to the current user. Once you get past the ViewState reconstruction, then initiate your reconciliation process - whether that is automatic or needs to load another UI to allow the user to participate is outside the scope of the question.

The point is that you need to keep "branches" of the master on a per-user basis and commit and read in a more controlled manner.

Rex M