DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Asynchronous Page in ASP.NET 2.0 and C#

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

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();
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.


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));
}
}

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.




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

Looking for more ASP.NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!