Set the ViewStateMode
to Disabled
on the page, and omit EnableViewState
. You can set it to enabled for some controls (default is Inherit
).
Edit (sidenote, see comments too):
As we discussed in the comment, text boxes keep their value even though ViewState is disabled. This is true, as they are elements of a HTTP POST request. Labels aren't, for instance, as they render to span
tags.
In the following sample code, there is a label filled with a random number. If you set ViewStateMode
to Enabled
, the random number from the last request is written to the Debug Output Window. If you set ViewStateMode
to Disabled
, an empty string is written. The Label
's state is not kept:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="WebApplication2.Default"
ViewStateMode="Disabled" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox runat="server" ID="textbox" Text="Default" />
<asp:Label runat="server" ID="randomNumberLabel" />
<asp:Button runat="server" Text="Click Me" />
</form>
</body>
</html>
This is the code behind. Be sure to attach a debugger:
namespace WebApplication2
{
public partial class Default : System.Web.UI.Page
{
private readonly static Random rnd = new Random();
protected void Page_Load(object sender, EventArgs e)
{
if(this.IsPostBack)
Debug.WriteLine(this.randomNumberLabel.Text);
this.randomNumberLabel.Text = rnd.Next(Int32.MaxValue).ToString();
}
}
}
I hope I managed to clarify the difference.