ASP.Net : Using a STRUCT to make working with ViewState easier

Last week I wrote an article suggesting cleaner ways of using ViewState to store local page variable state. I got some great feedback on that post and this blog is an extension on the topic that I came up with while experimenting with the various techniques brought up.

One of the things I look for in page level coding techniques is simplicity. "Write less, do more", but keep it simple. By simple, I mean easy to write and easy for someone else to read and maintain.

One of the challenges of using ViewState in a simple and efficient way is that it can get wordy. What I mean is with most approaches you end up typing in the variable name three or four times to set up a local variable and an associated property or you set up a local variable and then save and load it during SaveViewState and LoadViewState. All in all, for every variable you end up with multiple lines of declarative code.

After poking it over and over I came up with the idea to use a struct to remove the need for multiple lines of code per variable. Here's the result:

[Serializable()]
protected struct ViewStateStruct
{
 public int EmployeeID;
 public byte[] Version;
}
protected ViewStateStruct _vs = new ViewStateStruct();
protected override object SaveViewState() 
 { this.ViewState.Add("_vs", _vs); return base.SaveViewState(); }
protected override void LoadViewState(object savedState) 
 { base.LoadViewState(savedState); _vs = (ViewStateStruct)ViewState["_vs"]; }

As you can see, if you want to add a variable, you only have to declare it in one place inside the struct. Throughout your code all your ViewState variables are simply referenced by _vs.EmployeeID. If you don't like _vs, then it's easy to change it. Either way, be sure to set a standard name for the struct throughout your project.

In my previous blog on the topic, Tony pointed out an article on CodeProject called Using Attributes for encapsulating ASP.Net Session and ViewState variables. Combining this with the struct idea yields you with the following code in your page.

[Serializable()]
protected struct ViewStateStruct
{
 public int EmployeeID;
 public byte[] Version;
}
[PersistField(Location = PersistLocation.ViewState)]
protected ViewStateStruct _vs = new ViewStateStruct();

On the general topic of "write less, do more", there's one other thing I remembered while playing with this and Linq to SQL in the same page. I found when I went to load data from my Linq object to my form fields, I also needed to store the same value in my ViewState variable so that when the page came back, I'd have access to the original value. In C# you can do this:

tbName.Text = _vs.Name = employee.Name;

I seldom remember that but it's pretty useful.

Published Tuesday, October 09, 2007 10:31 AM by bschooley

Comments

# re: ASP.Net : Using a STRUCT to make working with ViewState easier

Tuesday, October 09, 2007 12:44 PM by Ryan Ternier

Usually when I work with ViewState I have public properties that acces the ViewState. This allows the custom controls I have on my page interact with the viewState using the parent page.

This is a great Idea if you have generic data that's always in the ViewState (say like a ControlID that you're editing).

# re: ASP.Net : Using a STRUCT to make working with ViewState easier

Tuesday, October 09, 2007 12:45 PM by Scott Galloway

Not sure why you've using a value type here...I can't see that it gains you anything as you have to box and unbox it anyway and of course the advice is that you have a maximum of 16 bytes in a value type anyway (from Framework Design Guidelines)...

# re: ASP.Net : Using a STRUCT to make working with ViewState easier

Tuesday, October 09, 2007 1:13 PM by bschooley

Hi Scott, I don't have to box or unbox anything. The system does it for me. Now, if I were going to do it repeatedly per page that might (and I emphasize might) be an issue, but at one per page. Besides, unless I'm missing something (quite possible), I don't have any choice here.

# To struct or not to struct

Tuesday, October 09, 2007 2:15 PM by mostlylucid

To struct or not to struct

# re: ASP.Net : Using a STRUCT to make working with ViewState easier

Thursday, October 11, 2007 11:24 PM by Ryan

Take a look at ViewState's tracking system. By always storing the values in viewstate at that point in the life cycle you are potentially wasting bandwidth.

# re: ASP.Net : Using a STRUCT to make working with ViewState easier

Saturday, October 13, 2007 9:20 AM by Bernal Schooley

Ryan, thanks for your comment. There are probably many people that think the same thing as we've been warned over and over again that viewstate is evil.

The truth is viewstate is a tool that is often not understood which leads to mis-use and problems. The technique I've described here isn't about what or how much to store, it's about how to do it.

The kind of data that I recommend storing with this technique is stuff that you must know when the rest of the data is posted back but that you don't need to show the user. Some good examples of this kind of data would be record identities or version timestamps. Many people use hiddenfields for this data, but that ends up storing the same data as html text and also duplicates the values in viewstate.

Ultimately, using the technique described here can actually reduce the amount of data your sending back and forth in viewstate when the data is something you need to exchange one way or another.

For more reading on the topic of viewstate, Dave Reed wrote a long but excellent article called Truly Understanding ViewState.

weblogs.asp.net/.../Truly-Understanding-Viewstate.aspx

Leave a Comment

(required) 
(required) 
(optional)
(required)