1、asp.net 1.1
1using System.Web.UI;
2using System.Web.UI.WebControls;
3using System.Web.UI.WebControls.WebParts;
4using System.Web.UI.HtmlControls;
5using System.Threading;
6public partial class Default2 : System.Web.UI.Page
7{
8 private int vlue = 0;
9 protected void Page_Load(object sender, EventArgs e)
10 {
11 Thread td = new Thread(new ThreadStart(BindData));
12 td.Start();
13 WritePress();
14 //BindData();
15 }
16 private void WritePress()
17 {
18 int i = 0;
19 while (vlue == 0)
20 {
21
22 Response.Write("<table id='s" + i + "' width='100%'><tr ><td></td><td align=center >正在读取数据请稍候.</td></tr></table>");
23 if (i > 0)
24 {
25 Response.Write("<script>document.getElementById('s" + (i - 1) + "').style.display ='none';</script>");
26 }
27 i = i + 1;
28 Response.Flush();
29
30 }
31 Response.Write("<script>document.getElementById('s" + (i - 1) + "').style.display ='none';</script>");
32 }
33 public void BindData()
34 {
35 dsOrders DSO = new dsOrders();
36 DSO.Orders.AddOrdersRow("zp", "1", DateTime.Now, DateTime.Now, DateTime.Now, "s", "t", "sz");
37 GridView1.DataSource = DSO;
38 GridView1.DataBind();
39 Thread.Sleep(700);
40 vlue = 1;
41 }
42}
2using System.Web.UI.WebControls;
3using System.Web.UI.WebControls.WebParts;
4using System.Web.UI.HtmlControls;
5using System.Threading;
6public partial class Default2 : System.Web.UI.Page
7{
8 private int vlue = 0;
9 protected void Page_Load(object sender, EventArgs e)
10 {
11 Thread td = new Thread(new ThreadStart(BindData));
12 td.Start();
13 WritePress();
14 //BindData();
15 }
16 private void WritePress()
17 {
18 int i = 0;
19 while (vlue == 0)
20 {
21
22 Response.Write("<table id='s" + i + "' width='100%'><tr ><td></td><td align=center >正在读取数据请稍候.</td></tr></table>");
23 if (i > 0)
24 {
25 Response.Write("<script>document.getElementById('s" + (i - 1) + "').style.display ='none';</script>");
26 }
27 i = i + 1;
28 Response.Flush();
29
30 }
31 Response.Write("<script>document.getElementById('s" + (i - 1) + "').style.display ='none';</script>");
32 }
33 public void BindData()
34 {
35 dsOrders DSO = new dsOrders();
36 DSO.Orders.AddOrdersRow("zp", "1", DateTime.Now, DateTime.Now, DateTime.Now, "s", "t", "sz");
37 GridView1.DataSource = DSO;
38 GridView1.DataBind();
39 Thread.Sleep(700);
40 vlue = 1;
41 }
42}
2、asp.net 2.0
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Threading;
11public partial class _Default : System.Web.UI.Page
12{
13 private AsyncDelegateGetDataSet dgs;
14 private int i = 0;
15
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 PageAsyncTask task = new PageAsyncTask(
19 new BeginEventHandler(BeginAsyncOperation),
20 new EndEventHandler(EndAsyncOperation),
21 new EndEventHandler(TimeoutAsyncOperation),
22 null
23 );
24 RegisterAsyncTask(task);
25 WritePress();
26 }
27 public void BindData()
28 {
29 dsOrders DSO = new dsOrders();
30 DSO.Orders.AddOrdersRow("zp", "1", DateTime.Now, DateTime.Now, DateTime.Now, "s", "t", "sz");
31 GridView1.DataSource = DSO;
32 GridView1.DataBind();
33 Thread.Sleep(3000);
34 }
35 IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
36 AsyncCallback cb, object state)
37 {
38
39 dgs=new AsyncDelegateGetDataSet(BindData);
40 return dgs.BeginInvoke(cb, state);
41 }
42 void EndAsyncOperation(IAsyncResult ar)
43 {
44 Response.Write("<script>document.getElementById('s" + (i - 1) + "').style.display ='none';</script>");
45 dgs.EndInvoke(ar);
46 }
47 void TimeoutAsyncOperation(IAsyncResult ar)
48 { }
49 private void WritePress()
50 {
51 while (i<3)
52 {
53
54 Response.Write("<table id='s" + i + "' width='100%'><tr ><td></td><td align=center >正在读取数据请稍候.</td></tr></table>");
55 if (i > 0)
56 {
57 Response.Write("<script>document.getElementById('s" + (i - 1) + "').style.display ='none';</script>");
58 }
59 i = i + 1;
60 Response.Flush();
61
62 }
63 }
64
65}
66public delegate void AsyncDelegateGetDataSet();
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Threading;
11public partial class _Default : System.Web.UI.Page
12{
13 private AsyncDelegateGetDataSet dgs;
14 private int i = 0;
15
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 PageAsyncTask task = new PageAsyncTask(
19 new BeginEventHandler(BeginAsyncOperation),
20 new EndEventHandler(EndAsyncOperation),
21 new EndEventHandler(TimeoutAsyncOperation),
22 null
23 );
24 RegisterAsyncTask(task);
25 WritePress();
26 }
27 public void BindData()
28 {
29 dsOrders DSO = new dsOrders();
30 DSO.Orders.AddOrdersRow("zp", "1", DateTime.Now, DateTime.Now, DateTime.Now, "s", "t", "sz");
31 GridView1.DataSource = DSO;
32 GridView1.DataBind();
33 Thread.Sleep(3000);
34 }
35 IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
36 AsyncCallback cb, object state)
37 {
38
39 dgs=new AsyncDelegateGetDataSet(BindData);
40 return dgs.BeginInvoke(cb, state);
41 }
42 void EndAsyncOperation(IAsyncResult ar)
43 {
44 Response.Write("<script>document.getElementById('s" + (i - 1) + "').style.display ='none';</script>");
45 dgs.EndInvoke(ar);
46 }
47 void TimeoutAsyncOperation(IAsyncResult ar)
48 { }
49 private void WritePress()
50 {
51 while (i<3)
52 {
53
54 Response.Write("<table id='s" + i + "' width='100%'><tr ><td></td><td align=center >正在读取数据请稍候.</td></tr></table>");
55 if (i > 0)
56 {
57 Response.Write("<script>document.getElementById('s" + (i - 1) + "').style.display ='none';</script>");
58 }
59 i = i + 1;
60 Response.Flush();
61
62 }
63 }
64
65}
66public delegate void AsyncDelegateGetDataSet();
在asp.net2.0的代码中采用了异步页,实现效率提高了。但是有一点不明白,一定要循环3次以上(while(i<3))前台才会显示“正在读取数据请稍候...”.