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 C#
Asynchronous Page in ASP.NET 2.0 and C#


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 C#.

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

Looking for the VB.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

using System.Text.RegularExpressions
using System.Net
using 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 void btnSubmit_Click(object sender, EventArgs e)
{
AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation));
}

'Async operation beginning

IAsyncResult BeginAsyncOperation(object sender,EventArgs e,AsyncCallback cb,object state)
{
_request = WebRequest.Create(this.txtUrl.Text.Trim());
return _request.BeginGetResponse(cb, state);
}
'Async operation ending


void EndAsyncOperation(IAsyncResult ar)
{
string text;
using (WebResponse response = _request.EndGetResponse(ar))
{
using(StreamReader reader = new StreamReader(response.GetResponseStream()))
{
text = reader.ReadToEnd();
}
}
Regex regex = new Regex("href\\s*=\\s*\"([^\"]*)\"",RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(text);
System.Text.StringBuilder builder = new System.Text.StringBuilder(1024);
foreach(Match match in matches)
{
builder.Append(match.Groups[1]);
builder.Append("
");
}
Output.Text = builder.ToString();
}

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.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text.RegularExpressions;
using System.IO;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
private WebRequest _request;
protected void Page_Load(object sender, EventArgs e)
{
AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation),new EndEventHandler(EndAsyncOperation));
this.lblUrl.Text = this.txtUrl.Text;
}
IAsyncResult BeginAsyncOperation(object sender,EventArgs e,AsyncCallback cb,object state)
{
_request = WebRequest.Create(this.txtUrl.Text.Trim());
return _request.BeginGetResponse(cb, state);
}
void EndAsyncOperation(IAsyncResult ar)
{
string text;
using (WebResponse response = _request.EndGetResponse(ar))
{
using(StreamReader reader = new StreamReader(response.GetResponseStream()))
{
text = reader.ReadToEnd();
}
}
Regex regex = new Regex("href\\s*=\\s*\"([^\"]*)\"",RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(text);
System.Text.StringBuilder builder = new System.Text.StringBuilder(1024);
foreach(Match match in matches)
{
builder.Append(match.Groups[1]);
builder.Append("<br>");
}
Output.Text = builder.ToString();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation));
}
}


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

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!
 





 
  Developer Resources







Server Intellect Rocks