頁面|執行 當在一個頁面執行時間比較長時,如大資料量的查詢等等。可採用下面的方法為使用者顯示提示。
用例情境:
使用者要產生XML檔案上報給國家局,而這個資料量是非常大量的,查詢資料一段時間,寫入XML檔案也需要一段時,那麼怎樣才能給使用者一個好的提示那。如當前正在分析上報資料。分析完成後,為使用者提示“當前正在寫入XML檔案”、“當前正在壓縮XML檔案”等等。在伺服器執行完一個階段後就給使用者一個提示。讓使用者知道自己所處在的位置。這樣會讓使用者知道沒有死機。
//在Page_Load方法中調用此方法。
private void loadplan()
{
string loadtext="正在載入頁面,請您耐心等待";
Response.Write("<div id='mydiv' width='400px'>");
Response.Write("_");
Response.Write("</div>");
Response.Write("<script>mydiv.innerText = '';</script>");
Response.Write("<script language=javascript>");
Response.Write("var output = '"+loadtext+"';");
Response.Write("var dots = 0;var dotmax = 10;function ShowWait()");
Response.Write("{dots++;if(dots>=dotmax)dots=1;");
Response.Write("for(var x = 0;x < dots;x++){output += '▌';}mydiv.innerText = output;}");
Response.Write("function StartShowWait(){mydiv.style.visibility = 'visible'; window.setInterval('ShowWait()',1000);}");
Response.Write("function HideWait(){mydiv.style.visibility = 'hidden';mydiv.style.display='none';window.clearInterval();}");
Response.Write("StartShowWait();</script>");
Response.Flush();
Thread.Sleep(10);
}
//在一個階段執行完成後調用此方法
//title 給使用者的提示
private void UpdatePlan(string title)
{
Response.Write("<script language=javascript>");
Response.Write("output = '"+title+"';");
Response.Write("</script>");
Response.Flush();
Thread.Sleep(10);
}
在HTML頁面中要有下面這個東東呀!
<script language="javascript">
<!--
HideWait();
//-->
</script>
例子:
//開始產生上報檔案
private void btnBuild_Click(object sender, System.EventArgs e)
{
this.UpdatePlan("正在查詢上報檔案所需要的資料,這可能需要一段時間,請您耐心等待!");//調用
System.Data.DataSet objs=this.buildobjs();
if(objs==null)return;
lnpt.DataAccessLayer.SystemCommon.UpLoadDAL dal=new lnpt.DataAccessLayer.SystemCommon.UpLoadDAL();
string uploadtype=this.Request.QueryString["uploadtype"];
string path=this.GetPath();
path=path+this.txtVersion.Text+this.txtAPP_CD.Text+System.DateTime.Now.ToFileTime().ToString()+".xml";
this.UpdatePlan("正在產生XML上報檔案,這可能需要一段時間,請您耐心等待!");//調用
dal.BuildXML(path,objs,uploadtype,this.txtVersion.Text,this.txtAPP_CD.Text,txtSEND_ORGAN.Text,this.txtRECE_ORGAN.Text,this.txtCONDITION.Text);
//添加上報檔案產生記錄
this.UpdatePlan("正在壓縮產生的XML上報檔案,請您耐心等待!");//調用
path=this.CreateZIPFile(path);
this.AddHistroy(path);
this.BindHistroy();
}