asp.net后台长时间操作时,向前台输出“请等待"信息的方法

      在asp.net中,有时候页面加载时,后台需要进行一些操作,比如查询数据库等。而且可能这些操作需要花费较长时间,致使前台长时间无响应,甚至有可能造成页面超时。可以采用以下方法解决:
      1、asp.net 1.1
      
 1 using  System.Web.UI;
 2 using  System.Web.UI.WebControls;
 3 using  System.Web.UI.WebControls.WebParts;
 4 using  System.Web.UI.HtmlControls;
 5 using  System.Threading;
 6 public  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
      
 1 using  System;
 2 using  System.Data;
 3 using  System.Configuration;
 4 using  System.Web;
 5 using  System.Web.Security;
 6 using  System.Web.UI;
 7 using  System.Web.UI.WebControls;
 8 using  System.Web.UI.WebControls.WebParts;
 9 using  System.Web.UI.HtmlControls;
10 using  System.Threading;
11 public  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}

66 public   delegate   void  AsyncDelegateGetDataSet();

      在asp.net2.0的代码中采用了异步页,实现效率提高了。但是有一点不明白,一定要循环3次以上(while(i<3))前台才会显示 正在读取数据请稍候... ”.

你可能感兴趣的:(asp.net)