Setting a Default Value on the "Get"
This is part two of a multi-part series on using ViewState to back your Page Properties. For those of you who read ViewState-backed Properties - Part One, you'll notice the code is pretty similar. The main difference is that instead of just returning a default value if one hasn't been set, we'll be setting one inside the "Get" portion of our Property. Once again we'll be using one of my favorite operators, the Null Coalescing Operator.
Consider the following code...
private string MyDefaultValue
{ get { return (string)(ViewState["MyDefaultValue "] ?? (ViewState["MyDefaultValue"] = "My default value")); } set { ViewState["MyDefaultValue "] = value; }}
Assuming nothing has been set in ViewState, the right side of the "??" will be evaluated. By wrapping the right side in parenthesis we're telling .NET to evaluate this expression first and then return what's been set. The effect is that not only is our default value returned but it is also persisted in ViewState.
Working Example
Below I've included a working page where we access ViewState directly and through our ViewState-backed Property. More to come in this series.... stay tuned.
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="DefaultAssignment.aspx.cs" Inherits="DefaultAssignment" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Default Value</title>
</head>
<body>
<form id="frmMain" runat="server">
<div>
<p>
When this button is clicked, ViewState will be accessed directly (ViewState["MyDefaultValue"]) and nothing will be displayed because it has not been set.
</p>
<asp:Label ID="lblDefaultValue" runat="server" Text="The value is: " />
<br />
<asp:Button ID="btnDefaultValue" runat="server" Text="Load Default" OnClick="btnDefaultValue_Click" />
</div>
<div>
<p>
When this button is clicked, instead of accessing ViewState directly we will access the ViewState-backed property causing it to be set and displayed.
</p>
<asp:Label ID="lblSetValue" runat="server" Text="The value is: " />
<br />
<asp:Button ID="btnSetValue" runat="server" Text="Set Value" OnClick="btnSetValue_Click" />
</div>
<p>
Clicking the first button again will now display the new set value as well even though we never used the "set" of the property.
</p>
</form>
</body>
</html>
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class DefaultAssignment : System.Web.UI.Page
{public DefaultAssignment() { Load += new EventHandler(Page_Load); }
protected void Page_Load(object sender, EventArgs e) { }
protected void btnDefaultValue_Click(object sender, EventArgs e)
{lblDefaultValue.Text = "The value is: " + ViewState["MyDefaultValue"];
}
protected void btnSetValue_Click(object sender, EventArgs e)
{ lblSetValue.Text = "The value is: " + MyDefaultValue ;}
private string MyDefaultValue
{ get { return (string)(ViewState["MyDefaultValue "] ?? (ViewState["MyDefaultValue"] = "My default value")); } set { ViewState["MyDefaultValue "] = value; }}
}