Page.IsPostBack 表示页面是否是从客户端返回。
假如有 a.aspx 和 b.aspx 两个页面,以下情况中,a.aspx 的 Page.IsPostBack 为 false。
- 在地址栏中输入 a.aspx,并访问。
- 在 b.aspx 中点击超链接链接到 a.aspx。
- b.aspx 通过 form 提交提交到 a.aspx。
以下代码,点击“测试”按钮,Page.IsPostBack 返回 true。
<%@ Page Language="C#" %>
<script runat="server">
void Test(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
tb.Text = "is postback";
}
else
{
tb.Text = "is not postback";
}
}
void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
lb.Text = "is postback";
}
else
{
lb.Text = "is not postback";
}
}
</script>
<!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>Page.IsPostBack</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tb" runat="server"></asp:TextBox>
<asp:Label ID="lb" runat="server"></asp:Label>
<asp:Button ID="btn" runat="server" Text="测试" OnClick="Test" />
</div>
</form>
</body>
</html>