Sys.WebForms.PageRequestManager initializeRequest 事件

更新:2007 年 11 月

在异步回发的初始化期间引发。

Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initializeRequestHandler)
Sys.WebForms.PageRequestManager.getInstance().remove_initializeRequest(initializeRequestHandler)

参数

定义

initializeRequestHandler

要调用的处理程序方法的名称。

备注

initializeRequest 方法在异步请求处理开始之前引发。可以使用此事件取消回发。

有关 PageRequestManager 事件模型的更多信息,请参见处理 PageRequestManager 事件

示例

下面的示例演示如何使用 initializeRequest 事件取消异步回发。initializeRequest 事件处理程序中的脚本使用 PageRequestManager 类的 isInAsyncPostBack 属性,确定异步回发是否正在进行。如果回发正在进行,则可使用 Sys.WebForms.InitializeRequestEventArgs 类的 postBackElement 属性,确定导致回发的元素的 ID。如果该 ID 与取消回发的按钮的 ID 相匹配,则调用 abortPostBack 方法。否则,使用 Sys.CancelEventArgs 类的 cancel 属性,取消当前请求。

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub ButtonClick_Handler(ByVal sender As Object, ByVal e As System.EventArgs)
        System.Threading.Thread.Sleep(3000)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>PageRequestManager initializeRequest Example</title>
    <style type="text/css">
    body {
        font-family: Tahoma;
    }
    a  {
        text-decoration: none;
    }
    a:hover {
        text-decoration: underline;
    }
    div.UpdatePanelStyle{
       width: 300px;
       height: 300px;
    }
    div.AlertStyle {
      font-size: smaller;
      background-color: #FFC080;
      height: 20px;
      visibility: hidden;
    }
    div.Container {
      display: inline;
      float: left;
      width: 330px;
      height: 300px;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <script type="text/javascript" language="javascript">
                var divElem = 'AlertDiv';
                var messageElem = 'AlertMessage';
                Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);
                function CheckStatus(sender, args)
                {
                  var prm = Sys.WebForms.PageRequestManager.getInstance();
                  if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id == 'CancelRefresh') {
                     prm.abortPostBack();
                  }
                  else if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id == 'RefreshButton') {
                     args.set_cancel(true);
                     ActivateAlertDiv('visible', 'Still working on previous request.');
                 }
                  else if (!prm.get_isInAsyncPostBack() & args.get_postBackElement().id == 'RefreshButton') {
                     ActivateAlertDiv('visible', 'Processing....');
                  }
                }
                function ActivateAlertDiv(visString, msg)
                {
                     var adiv = $get(divElem);
                     var aspan = $get(messageElem);
                     adiv.style.visibility = visString;
                     aspan.innerHTML = msg;
                }
            </script>
            <div class="Container" >
            <asp:UpdatePanel  ID="UpdatePanel1" UpdateMode="Conditional" runat="Server"  >
                <ContentTemplate>
                    <asp:Panel ID="Panel1" runat="server" GroupingText="UpdatePanel" 
                    CssClass="UpdatePanelStyle">
                        Last update: 
                        <%=DateTime.Now.ToString() %>.
                        <asp:Button runat="server" ID="RefreshButton" Text="Refresh"
                            OnClick="ButtonClick_Handler" />
                        <div id="AlertDiv" class="AlertStyle">
                        <span id="AlertMessage"></span> &nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:LinkButton ID="CancelRefresh" runat="server">cancel</asp:LinkButton>
                        </div>                        
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
            </div>
        </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void ButtonClick_Handler(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(3000);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>PageRequestManager initializeRequest Example</title>
    <style type="text/css">
    body {
        font-family: Tahoma;
    }
    a  {
        text-decoration: none;
    }
    a:hover {
        text-decoration: underline;
    }
    div.UpdatePanelStyle{
       width: 300px;
       height: 300px;
    }
    div.AlertStyle {
      font-size: smaller;
      background-color: #FFC080;
      height: 20px;
      visibility: hidden;
    }
    div.Container {
      display: inline;
      float: left;
      width: 330px;
      height: 300px;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <script type="text/javascript" language="javascript">
                var divElem = 'AlertDiv';
                var messageElem = 'AlertMessage';
                Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);
                function CheckStatus(sender, args)
                {
                  var prm = Sys.WebForms.PageRequestManager.getInstance();
                  if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id == 'CancelRefresh') {
                     prm.abortPostBack();
                  }
                  else if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id == 'RefreshButton') {
                     args.set_cancel(true);
                     ActivateAlertDiv('visible', 'Still working on previous request.');
                 }
                  else if (!prm.get_isInAsyncPostBack() & args.get_postBackElement().id == 'RefreshButton') {
                     ActivateAlertDiv('visible', 'Processing....');
                  }
                }
                function ActivateAlertDiv(visString, msg)
                {
                     var adiv = $get(divElem);
                     var aspan = $get(messageElem);
                     adiv.style.visibility = visString;
                     aspan.innerHTML = msg;
                }
            </script>
            <div class="Container" >
            <asp:UpdatePanel  ID="UpdatePanel1" UpdateMode="Conditional" runat="Server"  >
                <ContentTemplate>
                    <asp:Panel ID="Panel1" runat="server" GroupingText="UpdatePanel" 
                    CssClass="UpdatePanelStyle">
                        Last update: 
                        <%=DateTime.Now.ToString() %>.
                        <asp:Button runat="server" ID="RefreshButton" Text="Refresh"
                            OnClick="ButtonClick_Handler" />
                        <div id="AlertDiv" class="AlertStyle">
                        <span id="AlertMessage"></span> &nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:LinkButton ID="CancelRefresh" runat="server">cancel</asp:LinkButton>
                        </div>                        
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
            </div>
        </div>
    </form>
</body>
</html>

请参见

参考

Sys.WebForms.PageRequestManager 类

Sys.WebForms.PageRequestManager beginRequest 事件