This documentation is archived and is not being maintained.

Page.AddOnPreRenderCompleteAsync Method (BeginEventHandler, EndEventHandler)

Registers beginning and ending event handler delegates that do not require state information for an asynchronous page.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

'Declaration
Public Sub AddOnPreRenderCompleteAsync ( _
	beginHandler As BeginEventHandler, _
	endHandler As EndEventHandler _
)
'Usage
Dim instance As Page 
Dim beginHandler As BeginEventHandler 
Dim endHandler As EndEventHandler

instance.AddOnPreRenderCompleteAsync(beginHandler, _
	endHandler)

Parameters

beginHandler
Type: System.Web.BeginEventHandler

The delegate for the BeginEventHandler method.

endHandler
Type: System.Web.EndEventHandler

The delegate for the EndEventHandler method.

ExceptionCondition
InvalidOperationException

The <async> page directive is not set to true.

- or -

The AddOnPreRenderCompleteAsync method is called after the PreRender event.

ArgumentNullException

The BeginHandler or EndHandler is Nothing.

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. If you want to process multiple asynchronous methods simultaneously, you should use a single BeginEventHandler method and launch multiple asynchronous operations from that handler.

The asynchronous handlers are called between the PreRender and PreRenderComplete events.

First, all Page events (through the PreRender event) are run, and then each registered BeginEventHandler method is called. When the handler completes, the corresponding EndEventHandler method is called. If there are multiple asynchronous handlers, the next handler is called.

After the registered asynchronous event handlers have been called, the rest of the page events are called, beginning with the PreRenderComplete event.

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.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ 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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: