Page.AddOnPreRenderCompleteAsync Page.AddOnPreRenderCompleteAsync Page.AddOnPreRenderCompleteAsync Page.AddOnPreRenderCompleteAsync Method

定義

為非同步頁面,註冊開始與結束事件處理常式委派。Registers beginning and ending event handler delegates for an asynchronous page.

多載

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler)

為非同步頁面,註冊不需要狀態資訊的開始與結束事件處理常式委派。Registers beginning and ending event handler delegates that do not require state information for an asynchronous page.

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object)

為非同步頁面,註冊開始與結束事件處理常式委派。Registers beginning and ending event handler delegates for an asynchronous page.

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler)

為非同步頁面,註冊不需要狀態資訊的開始與結束事件處理常式委派。Registers beginning and ending event handler delegates that do not require state information for an asynchronous page.

public:
 void AddOnPreRenderCompleteAsync(System::Web::BeginEventHandler ^ beginHandler, System::Web::EndEventHandler ^ endHandler);
public void AddOnPreRenderCompleteAsync (System.Web.BeginEventHandler beginHandler, System.Web.EndEventHandler endHandler);
member this.AddOnPreRenderCompleteAsync : System.Web.BeginEventHandler * System.Web.EndEventHandler -> unit
Public Sub AddOnPreRenderCompleteAsync (beginHandler As BeginEventHandler, endHandler As EndEventHandler)

參數

endHandler
EndEventHandler EndEventHandler EndEventHandler EndEventHandler

EndEventHandler 方法的委派。The delegate for the EndEventHandler method.

例外狀況

<async> 頁面指示詞未設定為 trueThe <async> page directive is not set to true.

-或--or- AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) 事件之後,呼叫 PreRender 方法。The AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) method is called after the PreRender event.

範例

下列程式碼範例會使用非同步要求,在TextBox控制項中顯示本機 Web 服務器預設網頁的 HTML 原始碼。The following code example uses an asynchronous request to display the HTML source code of the local Web server's default page in a TextBox control.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

<%@ page language="C#" Async="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  System.Net.WebRequest myRequest;

  void Page_Load(object sender, EventArgs e)
  {
    Label1.Text = "Page_Load: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();

    BeginEventHandler bh = new BeginEventHandler(this.BeginGetAsyncData);
    EndEventHandler eh = new EndEventHandler(this.EndGetAsyncData);

    AddOnPreRenderCompleteAsync(bh, eh);

    // Initialize the WebRequest.
    string address = "http://localhost/";

    myRequest = System.Net.WebRequest.Create(address);
  }

  IAsyncResult BeginGetAsyncData(Object src, EventArgs args, AsyncCallback cb, Object state)
  {
    Label2.Text = "BeginGetAsyncData: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();
    return myRequest.BeginGetResponse(cb, state);
  }

  void EndGetAsyncData(IAsyncResult ar)
  {
    Label3.Text = "EndGetAsyncData: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();

    System.Net.WebResponse myResponse = myRequest.EndGetResponse(ar);

    result.Text = new System.IO.StreamReader(myResponse.GetResponseStream()).ReadToEnd();
    myResponse.Close();
  }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>
      Page.AddOnPreRenderCompleteAsync Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:label id="Label1" runat="server">
        Label 1</asp:label><br />
      <asp:label id="Label2" runat="server">
        Label 2</asp:label><br />
      <asp:label id="Label3" runat="server">
        Label 3</asp:label><br />
      <asp:textbox id="result" runat="server" textMode="multiLine" ReadOnly="true" columns="80" rows="25" />
    </form>
  </body>
</html>
<%@ page language="VB" Async="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Dim myRequest As System.Net.WebRequest
  
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = "Page_Load: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
    
    Dim bh As New BeginEventHandler(AddressOf Me.BeginGetAsyncData)
    Dim eh As New EndEventHandler(AddressOf Me.EndGetAsyncData)
    
    Me.AddOnPreRenderCompleteAsync(bh, eh)
    
    ' Initialize the WebRequest object.
    Dim address As String
    address = "http://localhost/"
    myRequest = System.Net.WebRequest.Create(address)
    
  End Sub
  
  Function BeginGetAsyncData(ByVal src As Object, ByVal args As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object) As IAsyncResult
    Label2.Text = "BeginGetAsyncData: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
    Return Me.myRequest.BeginGetResponse(cb, state)
  End Function
  
  Sub EndGetAsyncData(ByVal ar As IAsyncResult)
    Label3.Text = "EndGetAsyncData: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
    
    Dim myResponse As System.Net.WebResponse
    myResponse = Me.myRequest.EndGetResponse(ar)
    
    Dim reader As New System.IO.StreamReader(myResponse.GetResponseStream())
    result.Text = reader.ReadToEnd()
    myResponse.Close()
  End Sub
  
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>
      Page.AddOnPreRenderCompleteAsync Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:label id="Label1" runat="server">
        Label 1</asp:label><br />
      <asp:label id="Label2" runat="server">
        Label 2</asp:label><br />
      <asp:label id="Label3" runat="server">
        Label 3</asp:label><br />
      <asp:textbox id="result" runat="server" textMode="multiLine" ReadOnly="true" columns="80" rows="25" />
    </form>
  </body>
</html>

備註

AddOnPreRenderCompleteAsync使用方法,將處理常式加入至非同步網頁。Use the AddOnPreRenderCompleteAsync method to add handlers to an asynchronous Web page.

您可以註冊多個非同步處理常式;不過,一次只會執行一個處理常式。You can register multiple asynchronous handlers; however, only one handler runs at a time. 如果您想要同時處理多個非同步方法,您應該使用單一BeginEventHandler方法,並從該處理常式啟動多個非同步作業。If you want to process multiple asynchronous methods simultaneously, you should use a single BeginEventHandler method and launch multiple asynchronous operations from that handler.

非同步處理常式會在PreRenderPreRenderComplete事件之間呼叫。The asynchronous handlers are called between the PreRender and PreRenderComplete events.

首先,所有Page事件( PreRender透過事件)都會執行,然後呼叫每個已BeginEventHandler註冊的方法。First, all Page events (through the PreRender event) are run, and then each registered BeginEventHandler method is called. 當處理常式完成時,會EndEventHandler呼叫對應的方法。When the handler completes, the corresponding EndEventHandler method is called. 如果有多個非同步處理常式,則會呼叫下一個處理常式。If there are multiple asynchronous handlers, the next handler is called.

呼叫已註冊的非同步事件處理常式之後,會從PreRenderComplete事件開始呼叫其餘的頁面事件。After the registered asynchronous event handlers have been called, the rest of the page events are called, beginning with the PreRenderComplete event.

另請參閱

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object) AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object)

為非同步頁面,註冊開始與結束事件處理常式委派。Registers beginning and ending event handler delegates for an asynchronous page.

public:
 void AddOnPreRenderCompleteAsync(System::Web::BeginEventHandler ^ beginHandler, System::Web::EndEventHandler ^ endHandler, System::Object ^ state);
public void AddOnPreRenderCompleteAsync (System.Web.BeginEventHandler beginHandler, System.Web.EndEventHandler endHandler, object state);
member this.AddOnPreRenderCompleteAsync : System.Web.BeginEventHandler * System.Web.EndEventHandler * obj -> unit
Public Sub AddOnPreRenderCompleteAsync (beginHandler As BeginEventHandler, endHandler As EndEventHandler, state As Object)

參數

endHandler
EndEventHandler EndEventHandler EndEventHandler EndEventHandler

EndEventHandler 方法的委派。The delegate for the EndEventHandler method.

state
Object Object Object Object

物件,包含事件處理常式的狀態資訊。An object containing state information for the event handlers.

例外狀況

<async> 頁面指示詞未設定為 trueThe <async> page directive is not set to true.

-或--or- AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) 事件之後,呼叫 PreRender 方法。The AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) method is called after the PreRender event.

範例

下列程式碼範例會使用非同步要求,在TextBox控制項中顯示本機 Web 服務器預設網頁的 HTML 原始碼。The following code example uses an asynchronous request to display the HTML source code of the local Web server's default page in a TextBox control.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

<%@ page language="C#" Async="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  System.Net.WebRequest myRequest;

  void Page_Load(object sender, EventArgs e)
  {
    Label1.Text = "Page_Load: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();

    BeginEventHandler bh = new BeginEventHandler(this.BeginGetAsyncData);
    EndEventHandler eh = new EndEventHandler(this.EndGetAsyncData);

    AddOnPreRenderCompleteAsync(bh, eh);

    // Initialize the WebRequest.
    string address = "http://localhost/";

    myRequest = System.Net.WebRequest.Create(address);
  }

  IAsyncResult BeginGetAsyncData(Object src, EventArgs args, AsyncCallback cb, Object state)
  {
    Label2.Text = "BeginGetAsyncData: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();
    return myRequest.BeginGetResponse(cb, state);
  }

  void EndGetAsyncData(IAsyncResult ar)
  {
    Label3.Text = "EndGetAsyncData: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();

    System.Net.WebResponse myResponse = myRequest.EndGetResponse(ar);

    result.Text = new System.IO.StreamReader(myResponse.GetResponseStream()).ReadToEnd();
    myResponse.Close();
  }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>
      Page.AddOnPreRenderCompleteAsync Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:label id="Label1" runat="server">
        Label 1</asp:label><br />
      <asp:label id="Label2" runat="server">
        Label 2</asp:label><br />
      <asp:label id="Label3" runat="server">
        Label 3</asp:label><br />
      <asp:textbox id="result" runat="server" textMode="multiLine" ReadOnly="true" columns="80" rows="25" />
    </form>
  </body>
</html>
<%@ page language="VB" Async="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Dim myRequest As System.Net.WebRequest
  
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = "Page_Load: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
    
    Dim bh As New BeginEventHandler(AddressOf Me.BeginGetAsyncData)
    Dim eh As New EndEventHandler(AddressOf Me.EndGetAsyncData)
    
    Me.AddOnPreRenderCompleteAsync(bh, eh)
    
    ' Initialize the WebRequest object.
    Dim address As String
    address = "http://localhost/"
    myRequest = System.Net.WebRequest.Create(address)
    
  End Sub
  
  Function BeginGetAsyncData(ByVal src As Object, ByVal args As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object) As IAsyncResult
    Label2.Text = "BeginGetAsyncData: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
    Return Me.myRequest.BeginGetResponse(cb, state)
  End Function
  
  Sub EndGetAsyncData(ByVal ar As IAsyncResult)
    Label3.Text = "EndGetAsyncData: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
    
    Dim myResponse As System.Net.WebResponse
    myResponse = Me.myRequest.EndGetResponse(ar)
    
    Dim reader As New System.IO.StreamReader(myResponse.GetResponseStream())
    result.Text = reader.ReadToEnd()
    myResponse.Close()
  End Sub
  
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>
      Page.AddOnPreRenderCompleteAsync Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:label id="Label1" runat="server">
        Label 1</asp:label><br />
      <asp:label id="Label2" runat="server">
        Label 2</asp:label><br />
      <asp:label id="Label3" runat="server">
        Label 3</asp:label><br />
      <asp:textbox id="result" runat="server" textMode="multiLine" ReadOnly="true" columns="80" rows="25" />
    </form>
  </body>
</html>

備註

AddOnPreRenderCompleteAsync使用方法,將需要狀態資訊的處理常式加入至非同步網頁。Use the AddOnPreRenderCompleteAsync method to add handlers that require state information to an asynchronous Web page. 傳入state參數的物件可以是您的應用程式所需的任何物件,以便在和endHandler參數中指定的beginHandler事件處理常式委派之間傳輸資訊。The object passed in the state parameter can be any object that your application requires to transfer information between event handler delegates specified in the beginHandler and the endHandler parameters.

您可以註冊多個非同步處理常式;不過,一次只會執行一個處理常式。You can register multiple asynchronous handlers; however, only one handler runs at a time. 如果您想要同時處理多個非同步方法,您應該使用單一BeginEventHandler方法,並從該處理常式啟動多個非同步作業。If you want to process multiple asynchronous methods simultaneously, you should use a single BeginEventHandler method and launch multiple asynchronous operations from that handler.

非同步處理常式會在PreRenderPreRenderComplete事件之間呼叫。The asynchronous handlers are called between the PreRender and PreRenderComplete events.

首先,所有Page事件( PreRender透過事件)都會執行,然後呼叫每個已BeginEventHandler註冊的方法。First, all Page events (through the PreRender event) are run, and then each registered BeginEventHandler method is called. 當處理常式完成時,會EndEventHandler呼叫對應的方法。When the handler completes, the corresponding EndEventHandler method is called. 如果有多個非同步處理常式,則會呼叫下一個處理常式。If there are multiple asynchronous handlers, the next handler is called.

呼叫已註冊的非同步事件處理常式之後,會從PreRenderComplete事件開始呼叫其餘的頁面事件。After the registered asynchronous event handlers have been called, the rest of the page events are called, beginning with the PreRenderComplete event.

適用於