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 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!
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 are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
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 <asp:Label ID="lblUrl" runat="server"> </asp:Label></span>:<br> <asp:Label ID="Output" runat="server"></asp:Label> </fieldset> </div> </form> </body> </html> |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
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)); } |
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
No comments:
Post a Comment