webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test6_10.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
年龄:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="保存" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="获取" OnClick="Button2_Click" /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace test6_10
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
ViewState["Name"] = TextBox1.Text;
ViewState["Age"] = TextBox2.Text;
}
protected void Button2_Click(object sender, EventArgs e)
{
if(ViewState["Name"] != string.Empty && ViewState["Age"] != string.Empty)
{
Label1.Text = ViewState["Name"] + " " + ViewState["Age"];
}
}
}
}