Viewstate

View State rules

Don't override control properties in OnInit

public class MyControl : WebControl
{    
public string Text 
{       get {            return this.ViewState["Text"] == null ? Session["SomeSessionKey"] : this.ViewState["Text"] as string;        }
         set { this.ViewState["Text"] = value; }    }}

For readonly server controls that are set dynamically always turn off Viewstate

<asp:Label id="NameLabel" runat="server" EnableViewState="false" />

Databinding must happen in ONInit to preserve state

i.e. you will lose selected item if you bind in OnLoad

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License