<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoViewState.aspx.cs" Inherits="WebApplication1.DemoViewState" %>
<!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">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="增长" />
<br />
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="加1" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class DemoViewState : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public int i = 0;
protected void Button1_Click(object sender, EventArgs e)
{
i++;
Response.Write(i);
}
protected void Button2_Click(object sender, EventArgs e)
{
//ViewState 视图状态对象,它的数据是存在在页面的那个隐藏域
if (ViewState["count"] == null)
{
ViewState["count"] = 10;
}
else
{
int x = Convert.ToInt32(ViewState["count"]);
x++;
ViewState["count"] = x;
}
Response.Write(ViewState["count"]);
}
}
}