Take a look at Server Controls retain their state when EnableViewState is set to False
The following server controls retain their information through requests, even if the ViewState control (EnableViewState attribute) is set to False:
- TextBox control.
- CheckBox control.
- RadioButton Control
This is because the ViewState of the control is only one of the methods that are used to store control attributes in queries. In the server controls mentioned, attributes that are not typically sent to the server via the get-form or message-form are processed by ViewState. These values โโinclude control attributes, such as BackColor.
Attributes that are typically sent to the server are handled by the IPostBackDataHandler interface. An example of such an attribute is the verified attribute of a CheckBox control.
Example : programmatically set the backcolor setting. In postback, if view mode is disabled, the background color of the Textbox control will be lost. However, the text value of the control is retained.
Note. If the backcolor was installed directly in the markup, and not in the code behind, it would be saved.
<form id="form1" runat="server"> <asp:TextBox ID="Textbox1" runat="server" EnableViewState="false"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" EnableViewState="false" /> </form> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { this.Textbox1.BackColor = Color.Yellow; } }
The following is an overview of the state of an ASP.NET view :
This is a common misconception among developers who view the state as somehow responsible for TextBoxes, CheckBoxes, DropDownLists, and other web controls remembering their values โโin the postback. This is not so, since values โโare identified by the values โโof the reverse form fields and are assigned in the LoadPostData () method for those controls that implement IPostBackDataHandler.
The server control may indicate that it is interested in checking pending data by implementing the IPostBackDataHandler interface . At this point in the life cycle of the page, the Page class enumerates posted back form fields and looks for the appropriate server control. If it finds a control, it checks to see if the control implements the IPostBackDataHandler interface. If so, it passes the appropriate postback data to the server control by calling the LoadPostData () method. The server element will then update its state based on postback data.
Also see below