共用方式為


Page.AddOnPreRenderCompleteAsync 方法

定義

為非同步頁面,註冊開始與結束事件處理常式委派。

多載

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler)

為非同步頁面,註冊不需要狀態資訊的開始與結束事件處理常式委派。

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object)

為非同步頁面,註冊開始與結束事件處理常式委派。

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler)

為非同步頁面,註冊不需要狀態資訊的開始與結束事件處理常式委派。

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)

參數

beginHandler
BeginEventHandler

BeginEventHandler 方法的委派。

endHandler
EndEventHandler

EndEventHandler 方法的委派。

例外狀況

<async> 頁面指示詞未設定為 true

-或-

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) 事件之後,呼叫 PreRender 方法。

範例

下列程式代碼範例會使用異步要求,在控件中 TextBox 顯示本機網頁伺服器默認頁面的 HTML 原始程式碼。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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使用方法,將處理程式新增至異步網頁。

您可以註冊多個異步處理程式;不過,一次只會執行一個處理程式。 如果您想要同時處理多個異步方法,您應該使用單 BeginEventHandler 一方法,並從該處理程式啟動多個異步操作。

異步處理程式會在和 PreRenderComplete 事件之間PreRender呼叫。

首先,會執行透過PreRender事件) (的所有Page事件,然後呼叫每個已註冊BeginEventHandler的方法。 當處理程式完成時,會呼叫對應的 EndEventHandler 方法。 如果有多個異步處理程式,則會呼叫下一個處理程式。

呼叫已註冊的異步事件處理程序之後,會從 事件開始 PreRenderComplete 呼叫頁面事件的其餘部分。

另請參閱

適用於

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler, Object)

為非同步頁面,註冊開始與結束事件處理常式委派。

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)

參數

beginHandler
BeginEventHandler

BeginEventHandler 方法的委派。

endHandler
EndEventHandler

EndEventHandler 方法的委派。

state
Object

物件,包含事件處理常式的狀態資訊。

例外狀況

<async> 頁面指示詞未設定為 true

-或-

AddOnPreRenderCompleteAsync(BeginEventHandler, EndEventHandler) 事件之後,呼叫 PreRender 方法。

範例

下列程式代碼範例會使用異步要求,在控件中 TextBox 顯示本機網頁伺服器默認頁面的 HTML 原始程式碼。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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使用 方法,將需要狀態資訊的處理程式新增至異步網頁。 傳入state參數的物件可以是應用程式在 和 參數中指定的beginHandlerendHandler事件處理程式委派之間傳送資訊所需的任何物件。

您可以註冊多個異步處理程式;不過,一次只會執行一個處理程式。 如果您想要同時處理多個異步方法,您應該使用單 BeginEventHandler 一方法,並從該處理程式啟動多個異步操作。

異步處理程式會在和 PreRenderComplete 事件之間PreRender呼叫。

首先,會執行透過PreRender事件) (的所有Page事件,然後呼叫每個已註冊BeginEventHandler的方法。 當處理程式完成時,會呼叫對應的 EndEventHandler 方法。 如果有多個異步處理程式,則會呼叫下一個處理程式。

呼叫已註冊的異步事件處理程序之後,會從 事件開始 PreRenderComplete 呼叫頁面事件的其餘部分。

適用於