« ICSharpCode.SharpZipLib access denied | Main | Use using to alias long generic type names »

Dynamically replacing controls, ViewState and PostData issues

For ViewState and PostData to work correctly when restoring state to the server controls during a POST to an ASP.NET aspx page, the UniqueID’s must be the same when state is restored as they were when state was saved..

I ran into trouble when I used a button’s click event handler to remove some controls and then added some replacement controls. The replacement controls were numbered sequentially starting after the original control numbering. When a postback occurred, my override of LoadViewState created only the replacement controls which were therefore numbered differently (since the original controls were never created). ViewState wasn’t being restored correctly and the checked property of CheckBox controls wasn’t being set correctly.

What’s missing from ASP.NET is a way of updating the naming scheme originated by the removed controls’ naming managers. If this were done, the replacement controls could be named consistently.

My workaround in this situation was to do a redirect back to the page with a query string argument that causes the correct new control layout to be created.

 

 

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)