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

      在asp.net中,有时候页面加载时,后台需要进行一些操作,比如查询数据库等。而且可能这些操作需要花费较长时间,致使前台长时间无响应,甚至有可能造成页面超时。可以采用以下方法解决:
      1、asp.net 1.1
      
 1None.gifusing System.Web.UI;
 2None.gifusing System.Web.UI.WebControls;
 3None.gifusing System.Web.UI.WebControls.WebParts;
 4None.gifusing System.Web.UI.HtmlControls;
 5None.gifusing System.Threading;
 6None.gifpublic partial class Default2 : System.Web.UI.Page
 7ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 8InBlock.gif    private int vlue = 0;
 9InBlock.gif    protected void Page_Load(object sender, EventArgs e)
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        Thread td = new Thread(new ThreadStart(BindData));
12InBlock.gif        td.Start();
13InBlock.gif        WritePress();
14InBlock.gif        //BindData();
15ExpandedSubBlockEnd.gif    }

16InBlock.gif    private void WritePress()
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18InBlock.gif        int i = 0;
19InBlock.gif        while (vlue == 0)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif  
22InBlock.gif            Response.Write("<table  id='s" + i + "' width='100%'><tr ><td></td><td align=center >正在读取数据请稍候dot.gif.</td></tr></table>");
23InBlock.gif            if (i > 0)
24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
25InBlock.gif                Response.Write("<script>document.getElementById('s" + (i - 1+ "').style.display ='none';</script>");
26ExpandedSubBlockEnd.gif            }

27InBlock.gif            i = i + 1;
28InBlock.gif            Response.Flush();
29InBlock.gif       
30ExpandedSubBlockEnd.gif        }

31InBlock.gif        Response.Write("<script>document.getElementById('s" + (i - 1+ "').style.display ='none';</script>");
32ExpandedSubBlockEnd.gif    }

33InBlock.gif    public void BindData()
34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
35InBlock.gif        dsOrders DSO = new dsOrders();
36InBlock.gif        DSO.Orders.AddOrdersRow("zp""1", DateTime.Now, DateTime.Now, DateTime.Now, "s""t""sz");
37InBlock.gif        GridView1.DataSource = DSO;
38InBlock.gif        GridView1.DataBind();
39InBlock.gif        Thread.Sleep(700);
40InBlock.gif        vlue = 1;
41ExpandedSubBlockEnd.gif    }
 
42ExpandedBlockEnd.gif}

      2、asp.net 2.0
      
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Web;
 5None.gifusing System.Web.Security;
 6None.gifusing System.Web.UI;
 7None.gifusing System.Web.UI.WebControls;
 8None.gifusing System.Web.UI.WebControls.WebParts;
 9None.gifusing System.Web.UI.HtmlControls;
10None.gifusing System.Threading;
11None.gifpublic partial class _Default : System.Web.UI.Page
12ExpandedBlockStart.gifContractedBlock.gifdot.gif{
13InBlock.gif    private AsyncDelegateGetDataSet dgs;
14InBlock.gif    private int i = 0;
15InBlock.gif
16InBlock.gif    protected void Page_Load(object sender, EventArgs e)
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18InBlock.gif        PageAsyncTask task = new PageAsyncTask(
19InBlock.gif            new BeginEventHandler(BeginAsyncOperation),
20InBlock.gif            new EndEventHandler(EndAsyncOperation),
21InBlock.gif            new EndEventHandler(TimeoutAsyncOperation),
22InBlock.gif            null
23InBlock.gif       );
24InBlock.gif        RegisterAsyncTask(task);
25InBlock.gif        WritePress();
26ExpandedSubBlockEnd.gif    }

27InBlock.gif     public void BindData()
28ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
29InBlock.gif        dsOrders DSO = new dsOrders();
30InBlock.gif        DSO.Orders.AddOrdersRow("zp""1", DateTime.Now, DateTime.Now, DateTime.Now, "s""t""sz");
31InBlock.gif        GridView1.DataSource = DSO;
32InBlock.gif        GridView1.DataBind();
33InBlock.gif        Thread.Sleep(3000);
34ExpandedSubBlockEnd.gif    }
   
35InBlock.gif    IAsyncResult BeginAsyncOperation(object sender, EventArgs e, 
36InBlock.gif        AsyncCallback cb, object state)
37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{     
38InBlock.gif
39InBlock.gif        dgs=new AsyncDelegateGetDataSet(BindData);
40InBlock.gif        return dgs.BeginInvoke(cb, state);
41ExpandedSubBlockEnd.gif    }

42InBlock.gif    void EndAsyncOperation(IAsyncResult ar)
43ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
44InBlock.gif        Response.Write("<script>document.getElementById('s" + (i - 1+ "').style.display ='none';</script>");
45InBlock.gif        dgs.EndInvoke(ar);
46ExpandedSubBlockEnd.gif    }

47InBlock.gif    void TimeoutAsyncOperation(IAsyncResult ar)
48ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{ }
49InBlock.gif    private void WritePress()
50ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
51InBlock.gif        while (i<3)
52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
53InBlock.gif
54InBlock.gif            Response.Write("<table  id='s" + i + "' width='100%'><tr ><td></td><td align=center >正在读取数据请稍候dot.gif.</td></tr></table>");
55InBlock.gif            if (i > 0)
56ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
57InBlock.gif                Response.Write("<script>document.getElementById('s" + (i - 1+ "').style.display ='none';</script>");
58ExpandedSubBlockEnd.gif            }

59InBlock.gif            i = i + 1;
60InBlock.gif            Response.Flush();
61InBlock.gif
62ExpandedSubBlockEnd.gif        }

63ExpandedSubBlockEnd.gif    }

64InBlock.gif
65ExpandedBlockEnd.gif}

66None.gifpublic delegate void AsyncDelegateGetDataSet();

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

转载于:https://www.cnblogs.com/zjzkiss/archive/2007/02/01/637473.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值