ViewState Cache

ViewState Cache

ViewState

  • An ASP.NET persistence mechanism that automatically embeds information about the page in a hidden field in the rendered HTML markup

ViewState Cache

  • An internal cache used to store information about the state of ASP.NET controls
  • It is assigned under a specific site in the web.config file
  • Provides a dictionary object for retaining values between multiple requests for the same page
  • Default method that the page uses to preserve page and control property values between round trips
  • Size is in bytes or you can append the value with KB, MB or GB
  • Set a size of 0 (zero) to disable the cache

ViewState Behavior

  • The current state of the page and controls is hashed and saved in a hidden field
  • The hash string can be stored in multiple hidden fields if needed
  • When the page is posted back to the server, the page parses the view-state string at page initialization and restores property information in the page

Site Setting Configuration Setting

  • <configuration>
    • <sitecore>
      • <sites>
        • <site name="shell" language="en" database="core" domain="sitecore" viewstatecachesize="200KB" />

Site Setting Default Configuration Setting

  • <configuration>
    • <sitecore>
      • <settings>
        • <setting name="Caching.DefaultViewStateCacheSize" value="5MB"/>

ViewState Cache C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

Sitecore.Sites.SiteContext site = Sitecore.Context.Site;
if (site == null) return;

string cacheViewState = Sitecore.Configuration.Settings.Caching.CacheViewState.ToString();
string defaultViewStateCacheSize = Sitecore.Configuration.Settings.Caching.DefaultViewStateCacheSize.ToString();
Sitecore.Caching.ViewStateCache viewCache = Sitecore.Caching.CacheManager.GetViewStateCache(site);
if (viewCache == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using &#58; for colon and &#63; for question mark and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", displayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Default ViewState Cache Size", "<b>" + defaultViewStateCacheSize + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ViewState Cache Name", "<b>" + viewCache.InnerCache.Name + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "ViewState Cache Enabled", "<b>" + viewCache.InnerCache.Enabled.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ViewState Cache Maximum Size", "<b>" + viewCache.InnerCache.MaxSize.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ViewState Cache Size", "<b>" + viewCache.InnerCache.Size.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ViewState Default Priority", "<b>" + viewCache.InnerCache.DefaultPriority.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ViewState Cache Remaining Space", "<b>" + viewCache.InnerCache.RemainingSpace.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ViewState Cache Count", "<b>" + viewCache.InnerCache.Count.ToString() + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "ViewState Cache Scavengable", "<b>" + viewCache.InnerCache.Scavengable.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ViewState Cache Sync Root", "<b>" + viewCache.InnerCache.SyncRoot.ToString() + "</b>");

output.RenderEndTag(); //Div


ViewState Cache C# Web Control Output

Default ViewState Cache Size: 5242880
ViewState Cache Name: website[viewstate]
ViewState Cache Enabled? False
ViewState Cache Maximum Size: 0
ViewState Cache Size: 0
ViewState Default Priority: Normal
ViewState Cache Remaining Space: 0
ViewState Cache Count: 0
ViewState Cache Scavengable? False
ViewState Cache Sync Root: System.Object

Sources