扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
(Asp.net 1.0一般处理过程) (使用Asp.net 2.0新增特性异步页功能处理过程) |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AsyncPage.aspx.cs" Inherits="_Default" Async="true" %> |
1private WebRequest _request; 2 protected void Page_Load(object sender, EventArgs e) 3 { 4 //注册异步调用的Begin和End方法. 5 AddOnPreRenderCompleteAsync( 6 new BeginEventHandler(BeginAsyncOperation), 7 new EndEventHandler(EndAsyncOperation) 8 ); 9 } 10 11 //异步调用开始方法(当执行此方法时,当前线程就回到线程池,等待为其它请求服务). 12 IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state) 13 { 14 _request = WebRequest.Create("http://blog.csdn.net/chengking/"); 15 return _request.BeginGetResponse(cb, state); 16 } 17 18 //异步调用结束后的接收方法(异步操作执行完成后,会重新从线程池中取个线程为本页面请求服务). 19 void EndAsyncOperation(IAsyncResult ar) 20 { 21 string text; 22 using (WebResponse response = _request.EndGetResponse(ar)) 23 { 24 using (StreamReader reader = new StreamReader(response.GetResponseStream())) 25 { 26 text = reader.ReadToEnd(); 27 } 28 } 29 this.lbOupput.Text = text; 30 } |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AsyncPage.aspx.cs" Inherits="_Default" Async="true" %> |
1public partial class AsyncVisitDatabase : System.Web.UI.Page 2{ 3 //定义数据操作对象 4 private SqlConnection _connection; 5 private SqlCommand _command; 6 private SqlDataReader _reader; 7 8 protected void Page_Load(object sender, EventArgs e) 9 { 10 if (!IsPostBack) 11 { 12 //注册事件Page_PreRender执行完成时执行方法 13 this.PreRenderComplete += new EventHandler(Page_PreRenderComplete); 14 15 /**////注册异步调用的Begin和End方法. 16 AddOnPreRenderCompleteAsync( 17 new BeginEventHandler(BeginAsyncOperation), 18 new EndEventHandler(EndAsyncOperation) 19 ); 20 } 21 } 22 23 //异步调用开始方法(当执行此方法时,当前线程就回到线程池,等待为其它请求服务). 24 IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state) 25 { 26 string connect = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 27 _connection = new SqlConnection(connect); 28 _connection.Open(); 29 _command = new SqlCommand("select * from sales", _connection); 30 return _command.BeginExecuteReader(cb, state); 31 } 32 33 //异步调用结束后的接收方法(异步操作执行完成后,会重新从线程池中取个线程为本页面请求服务). 34 void EndAsyncOperation(IAsyncResult ar) 35 { 36 _reader = _command.EndExecuteReader(ar); 37 } 38 39 //事件Page_PreRender执行完成时执行方法,在这里可以将异步调用返回结果赋值给页面上的控件或者其它善后操作. 40 protected void Page_PreRenderComplete(object sender, EventArgs e) 41 { 42 GridView1.DataSource = _reader; 43 GridView1.DataBind(); 44 } 45 46 public override void Dispose() 47 { 48 if (_connection != null) 49 _connection.Close(); 50 base.Dispose(); 51 } 52} |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AsyncPage.aspx.cs" Inherits="_Default" Async="true" %> |
1public class WebService : System.Web.Services.WebService { 2 3 public WebService () { 4 5 //Uncomment the following line if using designed components 6 //InitializeComponent(); 7 } 8 9 [WebMethod] 10 public DataSet GetData() { 11 return CreateData(); 12 } 13 14 private DataSet CreateData() 15 { 16 DataTable dtTypeChild = new DataTable(); 17 dtTypeChild.Columns.Add(new DataColumn("TypeID", typeof(int))); 18 dtTypeChild.Columns.Add(new DataColumn("TypeDetail", typeof(string))); 19 //Add data 20 DataRow drChild1 = dtTypeChild.NewRow(); 21 drChild1["TypeID"] = 1; 22 drChild1["TypeDetail"] = "Apple"; 23 dtTypeChild.Rows.Add(drChild1); 24 DataRow drChild2 = dtTypeChild.NewRow(); 25 drChild2["TypeID"] = 2; 26 drChild2["TypeDetail"] = "orange"; 27 dtTypeChild.Rows.Add(drChild2); 28 DataRow drChild3 = dtTypeChild.NewRow(); 29 drChild3["TypeID"] = 3; 30 drChild3["TypeDetail"] = "banana"; 31 dtTypeChild.Rows.Add(drChild3); 32 DataRow drChild4 = dtTypeChild.NewRow(); 33 drChild4["TypeID"] = 4; 34 drChild4["TypeDetail"] = "pineapple"; 35 dtTypeChild.Rows.Add(drChild4); 36 DataRow drChild5 = dtTypeChild.NewRow(); 37 drChild5["TypeID"] = 5; 38 drChild5["TypeDetail"] = "pear"; 39 dtTypeChild.Rows.Add(drChild5); 40 dtTypeChild.TableName = "fruit"; 41 42 DataSet ds = new DataSet(); 43 ds.Tables.Add(dtTypeChild); 44 return ds; 45 } 46 47} |
1public partial class AsyncVisitWebservice : System.Web.UI.Page 2{ 3 private King.WebService _ws; 4 private DataSet _ds; 5 6 protected void Page_Load(object sender, EventArgs e) 7 { 8 if (!IsPostBack) 9 { 10 this.PreRenderComplete += new EventHandler(Page_PreRenderComplete); 11 _ws = new King.WebService(); 12 _ws.GetDataCompleted += new King.GetDataCompletedEventHandler(GetDataCompleted); 13 _ws.Url = new Uri(Request.Url, "WebService.asmx").ToString(); 14 _ws.UseDefaultCredentials = true; 15 _ws.GetDataAsync(); 16 } 17 } 18 19 void GetDataCompleted(Object source, King.GetDataCompletedEventArgs e) 20 { 21 _ds = e.Result; 22 } 23 24 protected void Page_PreRenderComplete(object sender, EventArgs e) 25 { 26 this.GridView1.DataSource = _ds; 27 GridView1.DataBind(); 28 } 29 30 public override void Dispose() 31 { 32 if (_ws != null) 33 _ws.Dispose(); 34 base.Dispose(); 35 } 36} |
1public partial class AsyncPageTask : System.Web.UI.Page 2{ 3 private WebRequest _request; 4 5 protected void Page_Load(object sender, EventArgs e) 6 { 7 PageAsyncTask task = new PageAsyncTask( 8 new BeginEventHandler(BeginAsyncOperation), 9 new EndEventHandler(EndAsyncOperation), 10 new EndEventHandler(TimeoutAsyncOperation), 11 null 12 ); 13 14 RegisterAsyncTask(task); 15 } 16 17 IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state) 18 { 19 _request = WebRequest.Create("http://blog.csdn.net/chengking"); 20 return _request.BeginGetResponse(cb, state); 21 } 22 23 void EndAsyncOperation(IAsyncResult ar) 24 { 25 string text; 26 using (WebResponse response = _request.EndGetResponse(ar)) 27 { 28 using (StreamReader reader = new StreamReader(response.GetResponseStream())) 29 { 30 text = reader.ReadToEnd(); 31 } 32 } 33 34 lbDisplay.Text = text.ToString(); 35 } 36 37 //时间超时执行操作 38 void TimeoutAsyncOperation(IAsyncResult ar) 39 { 40 lbDisplay.Text = "Failture!"; 41 } 42} |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者