为 AJAX 请求添加客户端事件

      实例源代码: RequestEvent.rar
      一、添加/删除请求开始事件的方法:
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对象。关于这个类的详细信息请查看其 帮助,由于篇幅问题,这里不再过多的说它。

      三、创建实例。
            1、用.NET 2005创建ASP.NET AJAX-Enabled 网站。
            2、Default.aspx页面的代码如下:
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>

<! 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 id = " Head1 "  runat = " server " >
    
< title > PageRequestManager beginRequest Example </ 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 "   />

            
< 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 >

            3、Default.aspx.cs代码如下:
using  System;
using  System.Data;
using  System.Configuration;
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  _Default : 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);
    }

}

你可能感兴趣的:(Ajax)