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 显示本地 Web 服务器的默认页的 HTML 源代码。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 显示本地 Web 服务器的默认页的 HTML 源代码。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 传递的对象可以是应用程序在 中指定的 beginHandler 事件处理程序委托和 endHandler 参数之间传输信息所需的任何对象。

可以注册多个异步处理程序;但是,一次只运行一个处理程序。 如果要同时处理多个异步方法,则应使用单个 BeginEventHandler 方法并从该处理程序启动多个异步操作。

异步处理程序在 和 PreRenderComplete 事件之间PreRender调用。

首先,通过PreRender事件) (的所有Page事件都运行,然后调用每个已注册BeginEventHandler的方法。 处理程序完成后,将调用相应的 EndEventHandler 方法。 如果有多个异步处理程序,则调用下一个处理程序。

调用已注册的异步事件处理程序后,将调用其余页面事件,从 PreRenderComplete 事件开始。

适用于