BeginEventHandler Delegate
Assembly: System.Web (in system.web.dll)
public delegate IAsyncResult BeginEventHandler ( Object sender, EventArgs e, AsyncCallback cb, Object extraData )
/** @delegate */ public delegate IAsyncResult BeginEventHandler ( Object sender, EventArgs e, AsyncCallback cb, Object extraData )
Not applicable.
Parameters
- sender
The source of the event.
- e
An EventArgs that contains the event data.
- cb
The delegate to call when the asynchronous method call is complete. If cb is a null reference (Nothing in Visual Basic), the delegate is not called.
- extraData
Any additional data needed to process the request.
Return Value
The IAsyncResult that represents the result of the BeginEventHandler operation.When you create a BeginEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.
The following code example uses the BeginEventHandler delegate to register a handler an asynchronous page.
Security 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 (Visual Studio). |
<%@ 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>
Security Note: