天天看點

ASP.NET中Response.BufferOutput的使用技巧

  BufferOutput屬性用于擷取或設定一個值,該值訓示是否緩沖輸出并在處理完整個頁之後發送它。該屬性預設值是true,是以我們通常的頁面中的輸出内容都是在頁面處理完成之後才發送給用戶端由浏覽器呈現出來。如果頁面要處理的内容很多,可能很長時間才能看到頁面上的内容。這個時候有兩種方法可以讓資訊逐漸顯示,這樣就能知道目前程式運作到哪一步了。

  方法1:BufferOutput預設值為true,這個時候可以借助Flush方法和Clear方法将目前緩沖區的資訊輸出。這個方法比較通用,當你的頁面在處理一個複雜的程式的時候,可以即時輸出一些提示資訊。

 1         protected void Page_Load(object sender, EventArgs e)

 2         {

 3             if (!IsPostBack)

 4             {

 5                 ShowInfo("中", 100);

 6                 Response.Flush();

 7                 Response.Clear();

 8                 Thread.Sleep(1000);

 9 

10                 ShowInfo("國", 5);

11                 Response.Flush();

12                 Response.Clear();

13                 Thread.Sleep(1000);

14             }

15         }

16 

17         private void ShowInfo(string it, int count)

18         {

19             StringBuilder sb = new StringBuilder();

20             for (int i = 0; i < count; i++)

21             {

22                 sb.Append(it);

23             }

24             sb.Append("<br/>");

25             Response.Write(sb.ToString());

26         }

          方法2:把BufferOutput值設為false,這個時候緩沖區的内容會立即發送給用戶端顯示。這種方法有性能問題,因為沒有使用緩沖輸出,不過代碼簡潔一些。

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                Response.BufferOutput = false;

                ShowInfo("中", 100);

                Thread.Sleep(1000);

                ShowInfo("國", 5);

            }

        }

        private void ShowInfo(string it, int count)

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < count; i++)

                sb.Append(it);

            sb.Append("<br/>");

            Response.Write(sb.ToString());

          在使用的時候有兩個問題需要注意:

    一是IE浏覽器在緩沖區資料不少于256位元組的情況下才會輸出到用戶端,經測試FF浏覽器沒有這個問題。

          二是UFT8編碼的漢字一般占三個位元組(Unicode編碼是兩個位元組),這裡第一次輸出100個漢字是為了保證至少有256個位元組的資料。

<a target="_blank" href="http://www.cnblogs.com/lhb25/p/must-read-links-for-web-designers-and-developers-volume-12.html">Web 前端工程師和設計師必讀精華文章推薦</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/07/28/html5-awesome-single-page-sites-inspiration.html" target="_blank">酷!15個精美的 HTML5 單頁網站作品欣賞</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/11/22/best-awesome-css3-animation-demos.html">炫!35個讓人驚訝的 CSS3 動畫效果示範</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/03/02/30-mind-blowing-parallax-scrolling-effect-websites.html" target="_blank">贊!30個與衆不同的優秀視差滾動效果網站</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/01/13/25-outstanding-single-page-website-designs.html">靓å!25個優秀的國外單頁網站設計作品欣賞</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/09/awesome-html5-and-javascript-effects.html">帥!8個驚豔的 HTML5 和 JavaScript 特效</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/06/27/35-exclusive-rainbow-colored-flash-websites.html" target="_blank">頂!35個很漂亮的國外 Flash 網站作品欣賞</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/24/outstanding-admin-panels-part-one.html" target="_blank">哇!34個漂亮網站和應用程式背景管理界面</a>

ASP.NET中Response.BufferOutput的使用技巧

<a href="http://www.yyyweb.com/go/web" target="_blank">本部落格新站點 ◆ 前端裡 ◆ 歡迎圍觀:)</a>

歡迎任何形式的轉載,但請務必注明出處。

繼續閱讀