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);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s