ViewState Pattern

Example 1

<script runat="server">   

 public List<KeyValuePair<NotificationType, Boolean>> RegistrationDeltas
    {
        get
        {
            if (ViewState["RegistrationDeltas"] == null)
            {
                ViewState["RegistrationDeltas"] = new List<KeyValuePair<NotificationType, Boolean>>(); ;
            }
            return (List<KeyValuePair<NotificationType, Boolean>>)ViewState["RegistrationDeltas"];
        }
        set { ViewState["RegistrationDeltas"] = value; }
    }


...
..

Example 2

<script runat="server">   
public string IntroText {
		get {
			if(ViewState["IntroText"] == null) {
				ViewState["IntroText"] = "";
			}
			return (string)ViewState["IntroText"];
		}
		set { ViewState["IntroText"] = value; }
	}
...
..

Example 3

// Enables the ASP page to get/set the value of the text property and the value will be retained between the client and the server    public public string Text
{
   get 
   { 
      String s = (String)ViewState[“Text”];
       return ((s==null) ? String.Empty : s);
   }

set 
  {
    ViewState[“Text”] = value;  // values will be retained as it uses ViewState by default
  }
}


// RenderContents isn’t responsible for writing Start Tag and End Tag, only the text content between the start and end tag
protected override void RenderContents(HtmlTextWriter output)
{
    output.Write(Text);
}

Using Conditional Operators Instead Of An If Statement

Replace this:

if (!String.IsNullOrEmpty(CurrentEmailFormat))
            {
                rblEmailFormat.SelectedValue = CurrentEmailFormat;
            }
            else
            {
                rblEmailFormat.SelectedValue = "H"; // default to HTML(H) if not already set

            }

With this:

//Prefil Email Format on inital load, if not set, default to HTML (H).

rblEmailFormat.SelectedValue = (String.IsNullOrEmpty(CurrentEmailFormat)) ? "H" : CurrentEmailFormat;

MSDN Reference