.aspx文件
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span><%=Count%></span>
<input type="submit" value="计算" />
</div>
</form>
</body>
.aspx.cs文件
public partial class ViewStateDemo : System.Web.UI.Page
{
public int Count { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
int count = 0;
if (ViewState["count"] != null)
{
count = Convert.ToInt32(ViewState["count"]);
count++;
Count = count;
}
ViewState["count"] = Count;
}
}