Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, this is what i got so far, TY Jeff for pointing me in the right direction:</p> <p>TestPage:</p> <pre><code>public partial class Pages_Test : BasePage { [ViewState] public String Name { get; set; } </code></pre> <p>BasePage:</p> <pre><code> #region Support ViewState Attribute BindingFlags _flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; protected override Object SaveViewState() { object _baseState = base.SaveViewState(); IDictionary&lt;string, object&gt; _pageState = new Dictionary&lt;string, object&gt; { { "base", _baseState } }; //Use reflection to get properties marked for viewstate foreach (PropertyInfo _property in GetType().GetProperties(_flags)) { if (_property.HasAttribute&lt;ViewState&gt;()) { object _value = _property.GetValue(this, _flags , null, null, null); _pageState.Add(new KeyValuePair&lt;string, object&gt;(_property.Name, _value)); } } return _pageState; } protected override void LoadViewState(Object savedState) { if (savedState != null) { var _pageState = (IDictionary&lt;string, object&gt;)savedState; if (_pageState.ContainsKey("base")) { base.LoadViewState(_pageState["base"]); } //use reflection to set properties foreach (PropertyInfo _property in GetType().GetProperties(_flags )) { if (_property.HasAttribute&lt;ViewState&gt;() &amp;&amp; _pageState.ContainsKey(_property.Name)) { object _value = _pageState[_property.Name]; _property.SetValue(this, _value, _flags , null, null, null); } } } } #endregion </code></pre> <p>Attribute:</p> <pre><code>/// &lt;summary&gt; /// This attribute is used by the BasePage to identify properties that should be persisted to ViewState /// Note: Private properties are not supported /// &lt;/summary&gt; [AttributeUsage(AttributeTargets.Property)] public class ViewState : Attribute { //Marker } </code></pre> <p>Helpers:</p> <pre><code>public static class PropertyExtension { public static Boolean HasAttribute&lt;T&gt;(this PropertyInfo property) { object[] attrs = property.GetCustomAttributes(typeof(T), false); return attrs != null &amp;&amp; attrs.Length == 1; } } </code></pre> <hr> <p><strong>EDIT</strong></p> <p>Jan has a valid point about performance, I did some profiling with the following results:</p> <pre><code> Without Attribute With Attribute Increase Slower % One Property First Load 0,004897899 0,010734255 0,005836356 219 Save, postback 0,002353861 0,010478008 0,008124147 445 Load, Postback 0,001488807 0,00627482 0,004786013 421 10 properties First Load 0,006184096 0,015288675 0,009104579 247 Save, postback 0,004061759 0,015052262 0,010990503 371 Load, Postback 0,0015708 0,005833074 0,004262274 371 % increase Avg Page. 0,902215714567075 0,00648 </code></pre> <p>On a Empty page the increase is considerable, but on an average page with a load of 1s this increase amounts to 0,01%.</p> <hr> <p><strong>Update</strong> : Using PostSharp, PostSharp4ViewState</p> <p>Step 1 : Make sure your website is precompiled</p> <p>Step 2 : Install <a href="http://www.postsharp.org/download" rel="nofollow noreferrer">PostSharp</a> and <a href="http://postsharp4viewstate.codeplex.com/" rel="nofollow noreferrer">PostSharp4ViewState</a></p> <p>Step 3 : Reference PostSharp.Public And PostSharp4ViewState</p> <p>Step 4 : Following is Code is now valid.</p> <pre><code> [Persist(Mode=PersistMode.ViewState)] private string _name; public String Name { get { return _name; } set { _name = value; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload