Click here to Skip to main content
15,400,390 members
Articles / Web Development / HTML
Technical Blog
Posted 1 Feb 2012


2 bookmarked

Dynamic ViewState in ASP.NET WebForms – Part 2

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Feb 2012CPOL1 min read
Dynamic ViewState in ASP.NET WebForms – Part 2

In my 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 to some 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 <a href="" title="DynamicViewState class">DynamicViewState</a> class.

First of all, I changed the constructor to accept a StateBag instance instead of the Page class so that the DynamicViewState class does not need to be nested inside base page class. Secondly, I added all the interfaces which are implemented by StateBag. This makes it fully compatible with StateBag class so we can simply rename the new property in base page class from ViewBag to ViewState. This way, you can leave the old code that uses ViewState and use the new approach for all new code. Here is a sample snippet:

public partial class SamplePage : BasePage
    protected void Page_Load(object sender, EventArgs e)
        if (IsPostBack)
            loadLabel.Text = string.Format("Load count: {0}", ViewState.loadCount);
            ViewState["loadCount"] = 0;
            loadLabel.Text = "Load count: 0";

Getting Started with DynamicViewState

If you would like to use DynamicViewState in your code, the simplest way for getting it is using Nuget package available at Dynamic ViewState Nuget package. Once you add the package to your project, simply change the base class of your pages to BasePage and that is all you need. The source code is available at github.

Performance Benchmarks

In a comment to the previous post, Jalpesh Vadgama asked about performance impact. I ran benchmark to compare dynamic implementation with ordinary ViewState usage. As expected, dynamic implementation is slower, but the difference is quite small:

  • Average milliseconds with dynamic: 0.0003
  • Average milliseconds without dynamic: 0.00016

As you can see, dynamic access is twice slower compared to ordinary ViewState usage.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Giorgi Dalakishvili
Software Developer
Georgia Georgia
No Biography provided

Comments and Discussions

Questionliked the article Pin
nursiko24-Feb-12 10:59
Membernursiko24-Feb-12 10:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.