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 Webinar Tuesday, August 26, 2014 1:00 PM EDT Customers are more empowered and connected than ever, and the customer's journey has grown more complex. Their expectations are growing and trust is diminishing as they may interact with multiple brands through web, mobile and social channels. Considering 70% of the buying process in a complex sale is already complete before prospects are willing to engage with a live salesperson -- it's critical to understand your customers and anticipate their needs.* …

  • Not long ago, security was viewed as one of the biggest obstacles to widespread adoption of cloud-based deployments for enterprise software solutions. However, the combination of advancing technology and an increasing variety of threats that companies must guard against is rapidly turning the tide. Cloud vendors typically offer a much higher level of data center and virtual system security than most organizations can or will build out on their own. Read this white paper to learn the five ways that cloud …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds