ASP.NET Tip: Create a ViewState Property

Learn how it works.

ASP.NET uses the ViewState to store data going back and forth to the server, eliminating the manual drudgery of reloading form controls and so forth. The ViewState can also be helpful for keeping your own state information, once you know how to read and write it. Using the ViewState can also eliminate nearly every case in which you would have used a hidden input field, a cookie, or a query string variable to pass information. It simply provides a cleaner way to pass your own information while your page is running.

Consider a simple situation that shows how this works in practice. In my applications, I typically have an ASP.NET page that lists my records, and then I have a user control to add and edit records of that type. The user control has an Add method and an Edit method, and the Edit method accepts the primary key of the record to edit. When the Edit method gets the primary key, it stores it in the ViewState so that when the user clicks the Save button, the user control knows which record to update. The following snippet of code illustrates this:

public void EditRecord(object recordID)
{
   SelectedRecordID = recordID;
   // Load record from database and show in control
}

protected object SelectedRecordID
{
   get
   {
      return ViewState["SelectedRecordID"];
   }
   set
   {
      ViewState["SelectedRecordID"] = value;
   }
}

The save code within the user control uses the SelectedRecordID property to save the changes back to the database. Because there's no query string or other way to move data from the "hosting" page to the user control, the ViewState fits the bill nicely.

You can also change the ViewState property to return a particular data type, such as an integer or other value. Before you return the value, make sure you first check that the ViewState value is not null (as shown below). This code sends back a -1 if there is no record selected, but you could return another appropriate value:

protected Int32 SelectedRecordID
{
   get
   {
      if (ViewState["SelectedRecordID"] != null)
         return Convert.ToInt32(ViewState["SelectedRecordID"]);
      else
         return -1;
   }
   set
   {
      ViewState["SelectedRecordID"] = value;
   }
}

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds