Server controls persist their state when EnableViewState is set to False

The following server controls persist their information across requests even when the control ViewState (the EnableViewState attribute) is set to False:

  1. The TextBox control.
  2. The CheckBox control.
  3. The RadioButton control.

Steps to explain above behaviour

1) Create a Web Site project
2) Add a web page to project  for example Default.aspx
3) Add below HTML code to Default.aspx page

<form id=”form1″ runat=”server”>
<div>
<label enableviewstate=”false” id=”lblTest” runat=”server”></label>
<textbox enableviewstate=”false” id=”txtTest” runat=”server”></textbox>
<button id=”Button1″ runat=”server” text=”Submit” type=”submit”></button></div>
</form>

4) Write below code in Page_Load event in Default.aspx.cs


(!IsPostBack)
if
{
lblTest.Text = “Test Label”;
txtTest.BackColor = Color.FromName(“Tomato”);
txtTest.Text = “Test TextBox”;
}

5) Save and compile your project, and then browse to the .aspx page.



 6) Now click on Submit button and observe the changes.


When you resubmit the page , Label Text is lost . Also , Textbox Backcolor is lost where as Textbox Text is retained

Leave a Reply