Page Properties with Viewstate done right
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;
}