Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Performance - Asynchronous Page in ASP.NET 2.0 and VB.NET
Asynchronous Page in ASP.NET 2.0 and VB.NET


ASP.NET Performance Tutorial

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.

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

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

Looking for more ASP.NET Tutorials? Click Here!

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

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>

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

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

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

Looking for more ASP.NET Tutorials? Click Here!






 
  Developer Resources







Server Intellect Rocks