PageRequestManager -beginRequest事件
参数 BeginRequestEventArgs
参数属性:request属性 获得WebRequest对象(用于请求的信息)
postBackElement 触发异步刷新的dom元素
常用操作:
--读取请求信息
--改变请求方式
--显示更新提示
如果updateprogress绑定了一个updatePanel的Id,则只有updatePanel内部的刷新请求能触发updateprogress的状态
如果外部触发了一个刷新请求,也需要updatepanel 显示状态,那么就需要手动写代码强制显示
先看看asp.net ajax的updateprogress实现原理:
- Sys.WebForms.PageRequestManager.registerClass('Sys.WebForms.PageRequestManager');
- Sys.UI._UpdateProgress = function Sys$UI$_UpdateProgress(element) {
- Sys.UI._UpdateProgress.initializeBase(this,[element]);
- this._displayAfter = 500;
- this._dynamicLayout = true;
- this._associatedUpdatePanelId = null;
- this._beginRequestHandlerDelegate = null;
- this._startDelegate = null;
- this._endRequestHandlerDelegate = null;
- this._pageRequestManager = null;
- this._timerCookie = null;
- }
- function Sys$UI$_UpdateProgress$get_displayAfter() {
- /// <value type="Number"></value>
- if (arguments.length !== 0) throw Error.parameterCount();
- return this._displayAfter;
- }
- function Sys$UI$_UpdateProgress$set_displayAfter(value) {
- var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
- if (e) throw e;
- this._displayAfter = value;
- }
- function Sys$UI$_UpdateProgress$get_dynamicLayout() {
- /// <value type="Boolean"></value>
- if (arguments.length !== 0) throw Error.parameterCount();
- return this._dynamicLayout;
- }
- function Sys$UI$_UpdateProgress$set_dynamicLayout(value) {
- var e = Function._validateParams(arguments, [{name: "value", type: Boolean}]);
- if (e) throw e;
- this._dynamicLayout = value;
- }
- function Sys$UI$_UpdateProgress$get_associatedUpdatePanelId() {
- /// <value type="String" mayBeNull="true"></value>
- if (arguments.length !== 0) throw Error.parameterCount();
- return this._associatedUpdatePanelId;
- }
- function Sys$UI$_UpdateProgress$set_associatedUpdatePanelId(value) {
- var e = Function._validateParams(arguments, [{name: "value", type: String, mayBeNull: true}]);
- if (e) throw e;
- this._associatedUpdatePanelId = value;
- }
- function Sys$UI$_UpdateProgress$_handleBeginRequest(sender, arg) {
- var curElem = arg.get_postBackElement();
- var showProgress = !this._associatedUpdatePanelId;
- while (!showProgress && curElem) {
- if (curElem.id && this._associatedUpdatePanelId === curElem.id) {
- showProgress = true;
- }
- curElem = curElem.parentNode;
- }
- if (showProgress) {
- this._timerCookie = window.setTimeout(this._startDelegate, this._displayAfter);
- }
- }
- //下面的方法就是UpdateProgress显示的实现
- //_dynamicLayout属性为true的时候,说明一个异步请求在提交
- //_dynamicLayout属性为 false的时候,说明请求结束,隐藏掉
- //我们可以模仿这个方法强制显示updatepregress
- //-------------------------------------------------------------------------------------------------------
- function Sys$UI$_UpdateProgress$_startRequest() {
- if (this._pageRequestManager.get_isInAsyncPostBack()) {
- if (this._dynamicLayout) this.get_element().style.display = 'block';
- else this.get_element().style.visibility = 'visible';
- }
- this._timerCookie = null;
- }
- //--------------------------------------------------------------------------------------------------------
- //下面函数调用了handleEndRequest所以我们不用做处理,异步刷新请求后直接调隐藏
- //--------------------------------------------------------------------------------------------------------
- function Sys$UI$_UpdateProgress$_handleEndRequest(sender, arg) {
- if (this._dynamicLayout) this.get_element().style.display = 'none';
- else this.get_element().style.visibility = 'hidden';
- if (this._timerCookie) {
- window.clearTimeout(this._timerCookie);
- this._timerCookie = null;
- }
- }
- //----------------------------------------------------------------------------------------------------------
- function Sys$UI$_UpdateProgress$dispose() {
- if (this._pageRequestManager !== null) {
- this._pageRequestManager.remove_beginRequest(this._beginRequestHandlerDelegate);
- this._pageRequestManager.remove_endRequest(this._endRequestHandlerDelegate);
- }
- Sys.UI._UpdateProgress.callBaseMethod(this,"dispose");
- }
- function Sys$UI$_UpdateProgress$initialize() {
- Sys.UI._UpdateProgress.callBaseMethod(this, 'initialize');
- this._beginRequestHandlerDelegate = Function.createDelegate(this, this._handleBeginRequest);
- this._endRequestHandlerDelegate = Function.createDelegate(this, this._handleEndRequest);
- this._startDelegate = Function.createDelegate(this, this._startRequest);
- if (Sys.WebForms && Sys.WebForms.PageRequestManager) {
- this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
- }
- if (this._pageRequestManager !== null ) {
- this._pageRequestManager.add_beginRequest(this._beginRequestHandlerDelegate);
- this._pageRequestManager.add_endRequest(this._endRequestHandlerDelegate);
- }
- }
- Sys.UI._UpdateProgress.prototype = {
- get_displayAfter: Sys$UI$_UpdateProgress$get_displayAfter,
- set_displayAfter: Sys$UI$_UpdateProgress$set_displayAfter,
- get_dynamicLayout: Sys$UI$_UpdateProgress$get_dynamicLayout,
- set_dynamicLayout: Sys$UI$_UpdateProgress$set_dynamicLayout,
- get_associatedUpdatePanelId: Sys$UI$_UpdateProgress$get_associatedUpdatePanelId,
- set_associatedUpdatePanelId: Sys$UI$_UpdateProgress$set_associatedUpdatePanelId,
- _handleBeginRequest: Sys$UI$_UpdateProgress$_handleBeginRequest,
- _startRequest: Sys$UI$_UpdateProgress$_startRequest,
- _handleEndRequest: Sys$UI$_UpdateProgress$_handleEndRequest,
- dispose: Sys$UI$_UpdateProgress$dispose,
- initialize: Sys$UI$_UpdateProgress$initialize
- }
- Sys.UI._UpdateProgress.registerClass('Sys.UI._UpdateProgress', Sys.UI.Control);
下面是资料中PageRequestManager -beginRequest事件的例子:强制显示UpdateProgress
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="AJAXEnabledWebApplication1.WebForm5" %>
- <!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>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <%=DateTime.Now%><br />
- <asp:Button ID="btnInUpdatePanel" runat="server" Text="ButtonInUpdatePanel" />
- </ContentTemplate>
- <Triggers>
- <asp:AsyncPostBackTrigger ControlID="btnOutUpdatePanel" />
- </Triggers>
- </asp:UpdatePanel>
- <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1" DynamicLayout="false">
- <ProgressTemplate>
- <div style="color:Red">Loading....</div>
- </ProgressTemplate>
- </asp:UpdateProgress>
- <hr />
- <asp:Button ID="btnOutUpdatePanel" runat="server" Text="ButtonOutUpdatePanel" />
- <script type="text/javascript" language="javascript">
- Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(
- function(sender,e){
- //如果发起异步刷新请求的buttonID不是外部的button return掉
- if(e.get_postBackElement().id!="<%=btnOutUpdatePanel.ClientID %>"){
- return;
- }
- //得到 updateProgress
- var updateProgress=$get("<%=this.UpdateProgress1.ClientID %>");
- //仿照微软的asp.net ajax库写~
- var dynamicLayout=<%=this.UpdateProgress1.DynamicLayout.ToString().ToLower() %>;
- if(dynamicLayout){
- updateProgress.style.display = "block";
- }
- else{
- updateProgress.style.visibility = "visible";
- }
- }
- );
- </script>
- </form>
- </body>
- </html>
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- namespace AJAXEnabledWebApplication1
- {
- public partial class WebForm5 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- this.btnInUpdatePanel.Click+=new EventHandler(btn_Click);
- this.btnOutUpdatePanel.Click+=new EventHandler(btn_Click);
- }
- protected void btn_Click(object sender, EventArgs e)
- {
- System.Threading.Thread.Sleep(3000);
- }
- }
- }
下篇将继续介绍这些事件~~