AJAX 客户端生命周期
1.PageRequestManager类
• getInstance静态方法
– 获得全局唯一的PageRequestManager实例
• isInAsyncPostBack属性
– 是否正处在一个异步更新过程中
• abortPostBack方法
– 取消当前的异步更新
– 不会产生异常
2.客户端生命周期
3. 代码范例:
ASPX:
Code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="5_HandleResponse.aspx.cs" Inherits="_5_HandleResponse" Title="HandleResponse" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<%= DateTime.Now %><br />
<asp:Button ID="btnError" runat="server" Text="Error" OnClick="btnError_Click" />
<asp:Button ID="btnTimeout" runat="server" Text="Timeout" OnClick="btnTimeout_Click" />
<input type="button" value="Abort" onclick="Sys.WebForms.PageRequestManager.getInstance().abortPostBack();" />
<asp:Button ID="btnDataItem" runat="server" Text="Register DataItem" OnClick="btnDataItem_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<script language="javascript" type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
function(sender, e)
{
if (e.get_error())
{
e.set_errorHandled(true);
if (e.get_response().get_timedOut())
{
showMessage("您的请求已超时。");
}
else if (e.get_response().get_statusCode() != 200)
{
showMessage("遇到未知错误。");
}
else
{
showMessage(e.get_error().message);
}
}
else if (e.get_response().get_aborted())
{
showMessage("您的请求已被取消。");
}
else
{
var upId = "<%= this.UpdatePanel1.ClientID %>";
var item = e.get_dataItems()[upId];
showMessage("您注册了:" + item);
}
});
</script>
</asp:Content>
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="5_HandleResponse.aspx.cs" Inherits="_5_HandleResponse" Title="HandleResponse" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<%= DateTime.Now %><br />
<asp:Button ID="btnError" runat="server" Text="Error" OnClick="btnError_Click" />
<asp:Button ID="btnTimeout" runat="server" Text="Timeout" OnClick="btnTimeout_Click" />
<input type="button" value="Abort" onclick="Sys.WebForms.PageRequestManager.getInstance().abortPostBack();" />
<asp:Button ID="btnDataItem" runat="server" Text="Register DataItem" OnClick="btnDataItem_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<script language="javascript" type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
function(sender, e)
{
if (e.get_error())
{
e.set_errorHandled(true);
if (e.get_response().get_timedOut())
{
showMessage("您的请求已超时。");
}
else if (e.get_response().get_statusCode() != 200)
{
showMessage("遇到未知错误。");
}
else
{
showMessage(e.get_error().message);
}
}
else if (e.get_response().get_aborted())
{
showMessage("您的请求已被取消。");
}
else
{
var upId = "<%= this.UpdatePanel1.ClientID %>";
var item = e.get_dataItems()[upId];
showMessage("您注册了:" + item);
}
});
</script>
</asp:Content>
CS:
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
public partial class _5_HandleResponse : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).AsyncPostBackTimeout = 3;
}
protected void btnError_Click(object sender, EventArgs e)
{
int two = 2;
int i = 3 / (two - 2);
}
protected void btnTimeout_Click(object sender, EventArgs e)
{
Thread.Sleep(5000);
}
protected void btnDataItem_Click(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).RegisterDataItem(this.UpdatePanel1, DateTime.Now.ToString());
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
public partial class _5_HandleResponse : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).AsyncPostBackTimeout = 3;
}
protected void btnError_Click(object sender, EventArgs e)
{
int two = 2;
int i = 3 / (two - 2);
}
protected void btnTimeout_Click(object sender, EventArgs e)
{
Thread.Sleep(5000);
}
protected void btnDataItem_Click(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).RegisterDataItem(this.UpdatePanel1, DateTime.Now.ToString());
}
}