Page Properties with Viewstate done right

Posted on: Thu Feb 25 11:55:03 -0800 2010. Updated on: Thu Feb 25 11:56:41 -0800 2010.
Category: DotNet

Here is an example of having page properties that hold their value in viewstate so that when the same screen posts back but in a different mode, ie. multistep processes, you can still hold the value. Also note we put the value into a local variable as well, because the viewstate will only be set after the postback, so therefore if you tried to access the property in the same request, you won't get a null back.

public partial class WizardSignup : System.Web.UI.Page
	{
public int? PhysicalDockId
		{
			get
			{
				if (ViewState["PhysicalDockId"] == null && _physicalDockId == null)
					return null;
				else
				{
					return _physicalDockId ?? (int)ViewState["PhysicalDockId"];
				}
			}
			set
			{
				ViewState["PhysicalDockId"] = (_physicalDockId = value);
			}
		}
		private int? _physicalDockId = null;
}

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.