Tuesday, 26 July 2011

Asynchronous Page in ASP.NET 2.0 and VB.NET


To asynchronous page in ASP.NET 2.0 can improve the whole performance of website for increasing users. This tutorial will show you how to asynchronous page by ASP.NET 2.0 and VB.NET.

First, import the namespace System.Text.RegularExpressions, System.IO, System.Net. The System.Text.RegularExpressions namespace contains classes that provide access to the.NET Framework regular expression engine. The namespace provides regular expression functionality that may be used from any platform or language that runs within the Microsoft.NET Framework.Asynchronous Page in Asp.Net2.0. Imports System.Text.RegularExpressions

Imports System.Text.RegularExpressions
Imports System.Net
Imports System.IO

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

We should add Async="true" in the <%page > code. Then we use the method of Page.AddOnPreRenderCompleteAsync to register beginning and ending event handler delegates that do not require state information for an asynchronous page.

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
AddOnPreRenderCompleteAsync(New BeginEventHandler(AddressOf BeginAsyncOperation), New EndEventHandler(AddressOf EndAsyncOperation))
End Sub

'Async operation beginning

Function BeginAsyncOperation(ByVal sender As Object, ByVal e As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object) As IAsyncResult
_request = WebRequest.Create(Me.txtUrl.Text.Trim())
Return _request.BeginGetResponse(cb, state)
End Function 'BeginAsyncOperation

'Async operation ending

Sub EndAsyncOperation(ByVal ar As IAsyncResult)
Dim [text] As String Dim response As WebResponse = _request.EndGetResponse(ar) Try
Dim reader As New StreamReader(response.GetResponseStream()) Try
[text] = reader.ReadToEnd()
Finally reader.Dispose() End Try
Finally
response.Close()
End Try Dim regex As New Regex("href\s*=\s*""([^""]*)""", RegexOptions.IgnoreCase) Dim matches As MatchCollection = regex.Matches([text]) Dim builder As New System.Text.StringBuilder(1024) Dim match As Match For Each match In matches
builder.Append(match.Groups(1)) builder.Append("<br>")
Next match Output.Text = builder.ToString()
End Sub 'EndAsyncOperation

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

The front end Default.aspx page looks something like this:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Default</title>
</head>
<body>

<form id="form1" runat="server">
<div>
<fieldset>

<legend>AsyncPage Demo</legend>
url:<asp:TextBox ID="txtUrl" runat="server" Width="200px">http://msdn.microsoft.com</asp:TextBox>

<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /><br />

<span style="color:Blue; font-weight:bold">Show hrefs in url &nbsp;

<asp:Label ID="lblUrl" runat="server">

</asp:Label></span>:<br>

<asp:Label ID="Output" runat="server"></asp:Label>
</fieldset>

</div>
</form>
</body>
</html>

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

The flow for the code behind page as follows.

Imports System.Text.RegularExpressions
Imports System.Net
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page
Dim _request As WebRequest
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddOnPreRenderCompleteAsync(New BeginEventHandler(AddressOf BeginAsyncOperation), New EndEventHandler(AddressOf EndAsyncOperation))
Me.lblUrl.Text = Me.txtUrl.Text
End Sub
Function BeginAsyncOperation(ByVal sender As Object, ByVal e As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object) As IAsyncResult
_request = WebRequest.Create(Me.txtUrl.Text.Trim())
Return _request.BeginGetResponse(cb, state)
End Function 'BeginAsyncOperation
Sub EndAsyncOperation(ByVal ar As IAsyncResult)
Dim [text] As String
Dim response As WebResponse = _request.EndGetResponse(ar)
Try
Dim reader As New StreamReader(response.GetResponseStream())
Try
[text] = reader.ReadToEnd()
Finally
reader.Dispose()
End Try
Finally
response.Close()
End Try
Dim regex As New Regex("href\s*=\s*""([^""]*)""", RegexOptions.IgnoreCase)
Dim matches As MatchCollection = regex.Matches([text])
Dim builder As New System.Text.StringBuilder(1024)
Dim match As Match
For Each match In matches
builder.Append(match.Groups(1))
builder.Append("<br>")
Next match
Output.Text = builder.ToString()
End Sub 'EndAsyncOperation
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub

Public Sub New()

End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
AddOnPreRenderCompleteAsync(New BeginEventHandler(AddressOf BeginAsyncOperation), New EndEventHandler(AddressOf EndAsyncOperation))
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment