C#實現頁面載入

iDotNetSpace發表於2009-07-29
很多情況,我們的頁面載入速度很慢,這時候就需要一個進度條來等待,增加使用者友好性.
原理比較簡單,在page_load的時候建立一個div,然後判斷頁面的載入裝載狀態,完成後消失.下面的程式碼:
C#實現頁面載入
 public void Loading()
        {
            HttpContext hc 
= HttpContext.Current;
            hc.Response.Write(
"
 ");
            hc.Response.Write(
"C#實現頁面載入 ");
            hc.Response.Write(
"
");
            
//hc.Response.Write("
            hc.Response.Write("
");
            hc.Response.Write(
"function document.onreadystatechange()");
            hc.Response.Write(
@"{ try  
                                   {
                                    //關鍵就是這句了,判斷頁面是否載入完畢
                                    if (document.readyState == 'complete') 
                                    {
                                         delNode('loading');
                                        
                                    }
                                   }
                                 catch(e)
                                    {
                                        alert('頁面載入失敗');
                                    }
                                                        } 
                            //刪除建立的div
                            function delNode(nodeId)
                            {   
                                try
                                {   
                                      var div =document.getElementById(nodeId); 
                                      if(div !==null)
                                      {
                                          div.parentNode.removeChild(div);   
                                          div=null;    
                                          CollectGarbage(); 
                                      } 
                                }
                                catch(e)
                                {   
                                   alert('刪除ID為'+nodeId+'的節點出現異常');
                                }   
                            }

                            
");

            hc.Response.Write(
"");
            hc.Response.Flush();
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-610728/,如需轉載,請註明出處,否則將追究法律責任。

相關文章