一、添加/删除请求开始事件的方法:
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对象。关于这 个类的详细信息请查看其帮助,由于篇幅问题,这里不再过多的说它。
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的代码如下:
这个实例使用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" % >
<! 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 > 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);
function BeginRequestHandler(sender, args)
{
var elem = args.get_postBackElement();
ActivateAlertDiv('visible', 'AlertDiv', elem.value + '被单击, 正在获取系统时间');
}
function EndRequestHandler(sender, args)
{
ActivateAlertDiv('hidden', 'AlertDiv', '');
}
function ActivateAlertDiv(visstring, elem, msg)
{
var adiv = $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 ="Update Panel的例子" >
最后一次更新时间:
< %= 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 >
< %@ Register Assembly ="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace ="System.Web.UI" TagPrefix ="asp" % >
<! 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 > 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);
function BeginRequestHandler(sender, args)
{
var elem = args.get_postBackElement();
ActivateAlertDiv('visible', 'AlertDiv', elem.value + '被单击, 正在获取系统时间');
}
function EndRequestHandler(sender, args)
{
ActivateAlertDiv('hidden', 'AlertDiv', '');
}
function ActivateAlertDiv(visstring, elem, msg)
{
var adiv = $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 ="Update Panel的例子" >
最后一次更新时间:
< %= 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, EventArgs e)
{
}
protected void ProcessClick_Handler( object sender, EventArgs e)
{
System.Threading.Thread.Sleep( 2000 );
}
}
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, EventArgs e)
{
}
protected void ProcessClick_Handler( object sender, EventArgs e)
{
System.Threading.Thread.Sleep( 2000 );
}
}
本文转自周金桥51CTO博客,原文链接:
http://blog.51cto.com/zhoufoxcn/167002
,如需转载请自行联系原作者