一、添加/删除请求开始事件的方法:
Sys.WebForms.PageRequestManager.instance.add_beginRequest(beginRequestHandler)
Sys.WebForms.PageRequestManager.instance.remove_beginRequest(beginRequestHandler)
参数beginRequestHandler指定当请求开始之前要调用的函数。
例:
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function BeginRequestHandler(sender, args)
{
//添加你想做的事情
}
函数参数args是Sys.WebForms.BeginRequestEventArgs类的一个实例。
BeginRequestEventArgs类包含两个成员
1、postBackElement 属性,发出触发请求的HTML对象。如: var elem = arg.get_postBackElement();
2、request 属性,获取当前请求的实例对象。如: var request = arg.get_request();
返回值是一个Sys.Net.WebRequest类型的实例,这里我们对它不做过多的讲述,请读者查阅更多的资料。
二、添加/删除请求结束的代码:
Sys.WebForms.PageRequestManager.instance.add_endRequest(endRequestHandler)
Sys.WebForms.PageRequestManager.instance.remove_endRequest(endRequestHandler)
参数endRequestHandler 指定当请求结束之后要调用的函数。例: Sys.WebForms.PageRequestManager.getInstance().add_endRequest
(EndRequestHandler);
function EndRequestHandler(sender, args)
{
//你想添加的代码
}
函数参数args是Sys.WebForms.EndRequestEventArgs类的实例。利用它可以获取请求期间发生的异常,以及请求返回的response对象。关于这 个类的详细信息请查看其帮助,由于篇幅问题,这里不再过多的说它。
三、演示实例
这个实例使用VS2005开发,注意还需要安装AJAX插件,插件名字是:ASPAJAXExtSetup.msi,可以到微软官方网站下载,也可以搜索一下,就近下载,安装之后会自动在VS2005工具箱上增加相关选项卡:
相关aspx的代码如下:
<
%@Page
Language
="C#"
AutoEventWireup
="true"
CodeFile
="AjaxDemo.aspx.cs"
Inherits
="AjaxDemo"
%
>
<
%@Register
Assembly
="System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"
Namespace
="System.Web.UI"
TagPrefix
="asp"
%
>
<!
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
Ajax事件管理代码
</
title
>
<
style
type
="text/css"
>
body
{
font-family:Tahoma;
}
div.AlertStyle
{
background-color:#FFC080;
top:95%;
left:1%;
height:20px;
position:absolute;
visibility:hidden;
}
</
style
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:scriptmanager
ID
="ScriptManager1"
runat
="server"
></
asp:scriptmanager
>
<
script
type
="text/javascript"
language
="javascript"
>
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
functionBeginRequestHandler(sender,args)
{
varelem=args.get_postBackElement();
ActivateAlertDiv('visible','AlertDiv',elem.value+'被单击,正在获取系统时间');
}
functionEndRequestHandler(sender,args)
{
ActivateAlertDiv('hidden','AlertDiv','');
}
functionActivateAlertDiv(visstring,elem,msg)
{
varadiv=$get(elem);
adiv.style.visibility=visstring;
adiv.innerHTML=msg;
}
</
script
>
<
asp:UpdatePanel
ID
="UpdatePanel1"
UpdateMode
="Conditional"
runat
="Server"
>
<
ContentTemplate
>
<
asp:Panel
ID
="Panel1"
runat
="server"
GroupingText
="UpdatePanel的例子"
>
最后一次更新时间:
<
%=DateTime
.Now.ToString()%
>
.
<
br
/>
<
asp:Button
runat
="server"
ID
="Button1"
Text
="第一个按钮"
OnClick
="ProcessClick_Handler"
/>
<
asp:Button
runat
="server"
ID
="Button2"
Text
="第二个按钮"
OnClick
="ProcessClick_Handler"
/>
</
asp:Panel
>
</
ContentTemplate
>
</
asp:UpdatePanel
>
<
div
id
="AlertDiv"
class
="AlertStyle"
>
</
div
>
</
div
>
</
form
>
</
body
>
</
html
>
相关asp.cs代码:
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;
public
partial
class
AjaxDemo:System.Web.UI.Page
{
protected
void
Page_Load(
object
sender,EventArgse)
{
}
protected
void
ProcessClick_Handler(
object
sender,EventArgse)
{
System.Threading.Thread.Sleep(
2000
);
}
}