Dynamic ViewState in ASP.Net WebForms – Part 2

In previous post I showed an approach combining DynamicObject with ViewState for accessing ViewState members using dynamic properties instead of string indexes. However that approach had some disadvantages: It is tied some a concrete base page class. It does not have all the properties/methods of StateBag class Mixing ViewState and ViewBag can be confusing for new developers. In order to overcome these issues I made several changes to the DynamicViewState class.

Dynamic ViewState in ASP.Net WebForms

If you have worked with ASP.Net MVC 3 you are probably aware of ViewBag property which allows you to pass data from Controller to View by using dynamic properties instead of hard coding strings. While the properties still are not strongly typed and compiler does not check them it can provide an advantage. For example you can avoid casting as it returns dynamic objects. This post will show how to use the same approach in ASP.Net Web Forms for ViewState property giving us a dynamic viewstate class.