共用方式為


Sys.WebForms.InitializeRequestEventArgs Class

Provides a class that is used by the initializeRequest event of the Sys.WebForms.PageRequestManager class to pass argument information to event handlers.

Namespace: Sys.WebForms

Inherits: Sys.CancelEventArgs

var args = new Sys.WebForms.InitializeRequestEventArgs(request, postBackElement, updatePanelsToUpdate);

Constructors

Name

Description

Sys.WebForms.InitializeRequestEventArgs Constructor

Initializes a new instance of the InitializeRequestEventArgs class.

Members

Name

Description

Sys.WebForms.InitializeRequestEventArgs postBackElement Property

Gets the postback element that initiated the asynchronous postback.

Sys.WebForms.InitializeRequestEventArgs request Property

Gets the request object that represents the current postback.

Sys.WebForms.InitializeRequestEventArgs.updatePanelsToUpdate Property

Gets or sets a list of UniqueID values for UpdatePanel controls that should re-render their content, as requested by the client.

Note

This class contains private members that support the client-script infrastructure and are not intended to be used directly from your code. Names of private members begin with an underscore ( _ ).

Remarks

Event handlers can use the request property to access the request object and use the postBackElement property to determine which element caused the postback.

The initializeRequest event of the PageRequestManager class is raised before processing of the asynchronous request starts.

Example

The following example shows how to use the initializeRequest event to enable an asynchronous postback to be canceled. Script in the initializeRequest event handler determines whether an asynchronous postback is currently in progress by using the isInAsyncPostBack property of the PageRequestManager class. If a postback is in progress, the postBackElement property is used to determine the ID of the element that caused the postback. If the ID matches the ID of a button that cancels the postback, the abortPostBack method of the PageRequestManager class is called. Otherwise, the current request is canceled by using the cancel property of the CancelEventArgs class.

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "https://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="https://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" 
  "https://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="https://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>

See Also

Reference

Sys.CancelEventArgs Class