How to Persist Object State Over Postbacks in ASP.NET WebForms
Posted by Randolph Cabral on Tuesday August 7, 2007
The PersistState Attribute
Persisting object state over postbacks in ASP.NET is a one line operation with Jammer.NET. Meet the PersistState attribute.
[PersistState] protected Customer _customer = null;
It may not seem like much on the surface, but the result is truly empowering. With that one line of code, the “_customer” object state is persisted in ViewState and restored on postback automatically.
Remember the amount of code you used to have to write just to save the customer object? Reload object state, then set property values to new values, then finally call save. With the PersistState attribute, you no longer need to make another round trip to the database to restore object state. Let’s examine the following example.
1 using System;
2 using Jmr.Web.UI;
3 using Northwind.Entities;
4
5 public partial class Pages_JmrWebForm : WebPageBase
6 {
7 [PersistState] protected Customer _customer = null;
8
9 protected void Page_Load(object sender, EventArgs e)
10 {
11 if (!IsPostBack)
12 {
13 _customer = new Customer(1);
14 _customer.Load(); // Loads customer object with values from db.
15 }
16 }
17
18 protected void btnSave_Click(object sender, EventArgs e)
19 {
20 _customer.ContactName = txtName.Text;
21 _customer.Save();
22 }
23 }
In this example, the “_customer” object is adorned with the PersistState attribute and is initialized to null. The “Page_Load” event handles loading the “_customer” object on initial page load by calling “Load()” on the entity (lines 13 and 14). Note also that lines 13 and 14 are only run the first time the page actually loads. The surrounding “if” statement ensures this by checking the “IsPostBack” page property.
Notice the “btnSave_Click” event handler does not need to create a new instance of the Customer object. Normally, the “_customer” object reference would be null and lines 13 and 14 would have to be repeated to prevent a null reference exception from getting thrown.
Using PersistState
In order to activate the PersistState feature, there are three elements that are required. First, make sure that the object to persist is declared at the class-level and marked protected. Secondly, adorn the new variable with the PersistState attribute. The PersistState attribute can be found in the “Jmr.Web.UI” namespace which is included as one of the using statements in the above example. Finally, make sure your web page derives from “Jmr.Web.UI.WebPageBase”. The “WebPageBase” class encapsulates all of the logic that makes object persistence possible.
Changing the Persistence Container with PersistScope
It’s important to note that any data that gets saved to a pages’ ViewState causes the size of the rendered mark-up to bloat. Because of this, ViewState may not always be the right container to persist object state. To change the persistence container, PersistState can accept a PersistScope enumeration value as a constructor argument. PersistScope values are as follows: Application, Cache, Session, and ViewState where ViewState is the default value if using the default constructor.
[PersistState(PersistScope.Session)] protected Customer _customer = null;

<–This screen capture illustrates the PersistScope enumeration and its’ values.
Please see MSDN’s ASP.NET State Management Overview for more information on state management in ASP.NET.