Asynchronous DataShow in Asp.Net 2.0 and C#
See more tutorials in Performance. This post has Comments Off on Asynchronous DataShow in Asp.Net 2.0 and C#.
Asynchronous DataShow in Asp.Net 2.0 will improve capability of the page. This tutorial will show you how to create Asynchronous DataShow in ASP.Net 2.0 and C#.
First we should add Async=”true” in the <%page > code.Then we use function Page. AddOnPreRenderCompleteAsync to register a function begin and function end.we use label control to show the title of table titles.
0 1 2 |
using System.Data.SqlClient |
Custom function BeginAsyncOperation ,EndAsyncOperation and Page_PreRenderComplete
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state) { string connectionstring = "server=localhost;uid=sa;pwd=1234;database=Pubs;Asynchronous Processing=true;"; _connection = new SqlConnection(connectionstring); _connection.Open(); _command = new SqlCommand("select title_id,title,price from titles", _connection); return _command.BeginExecuteReader(cb, state); } void EndAsyncOperation(IAsyncResult ar) { _reader = _command.EndExecuteReader(ar); } private void Page_PreRenderComplete(object sender, EventArgs e) { while (_reader.Read()) this.Label1.Text = this.Label1.Text + _reader.GetValue(1) + "<br>"; } |
The front page of Default.aspx
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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>AsyncDataBind</legend> <asp:Label ID="Label1" runat="server" Text="Title: <br>"></asp:Label> </fieldset> </div> </form> </body> </html> |
The whole code behind front page:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
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.Data.SqlClient; using System.Web.Configuration; public partial class _Default : System.Web.UI.Page { private SqlConnection _connection; private SqlCommand _command; private SqlDataReader _reader; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.PreRenderComplete += new EventHandler(Page_PreRenderComplete); Page.AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation)); } } IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state) { string connectionstring = "server=localhost;uid=sa;pwd=1234;database=Pubs;Asynchronous Processing=true;"; _connection = new SqlConnection(connectionstring); _connection.Open(); _command = new SqlCommand("select title_id,title,price from titles", _connection); return _command.BeginExecuteReader(cb, state); } void EndAsyncOperation(IAsyncResult ar) { _reader = _command.EndExecuteReader(ar); } private void Page_PreRenderComplete(object sender, EventArgs e) { while (_reader.Read()) this.Label1.Text = this.Label1.Text + _reader.GetValue(1) + "<br>"; } public override void Dispose() { if (_connection != null) _connection.Close(); base.Dispose(); } } |