Click here to Skip to main content
Licence CPOL
First Posted 1 Feb 2012
Views 6,395
Bookmarked 2 times

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.
A Technical Blog article. View original blog here.[^]

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 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.

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)
        {
            ViewState.loadCount++;

            loadLabel.Text = string.Format("Load count: {0}", ViewState.loadCount);
        }
        else
        {
            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. Source code is available at github.

Performance Benchmarks

In a comment to 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.

License

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

Member

Google+


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionliked the article Pinmembernursiko10:59 24 Feb '12  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 1 Feb 2012
Article Copyright 2012 by Giorgi Dalakishvili
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid