Introduction
在web application的表單提交過程中顯示「please wait」資訊或是gif動畫圖片通常是很有用的,特別是提交過程比較久的情況。我最近開發了一個調查提交程序,在程式裡內部使用者透過一個網頁上傳excel電子表格。程式將上傳的電子表格資料插入資料庫。這個過程只需要幾秒鐘,但即便是幾秒鐘,在網頁看來卻是非常明顯的等待過程。在程式測試的時候,有些用戶重複地點擊上傳按鈕。因此,提供一個視覺的資訊來告訴人們上傳正在進行中是很有用的。並同時把上傳按鈕一起隱藏掉,以防止多次點擊。這裡介紹的控制項是Button控制項的子類,它示範如何將客戶端javascript程式碼封裝在asp.net伺服器控制項中來提供便利的功能。
雖然外面已經有很多javascript的例子來完成這件事情,但當我試著把這些功能封裝到asp.net控制項中時我發現了一些問題。我最開始嘗試透過javascript的onclick句柄來使button無效,並用另外的文字取代。但我發現很棘手,這樣會妨礙到asp.net伺服器端的click事件的功能。而最終行得通的,並且對不同瀏覽器也有很好支援的方法是,讓button在div標記中呈現。 div可以隱藏且不妨礙asp.net的click事件。
Using the control
作為正常的button控制項的衍生,PleaseWaitButton的功能與它基本上相同。它透過三個附加的屬性來管理當按鈕被點擊後"please Wait"訊息或圖片的顯示。
PleaseWaitText
這是顯示的客戶端文字訊息,如果存在,當按鈕被點擊它將取代按鈕。
PleaseWaitImage
這是顯示的圖像檔案(例如gif動畫圖像),如果存在,當按鈕被點擊它將取代按鈕。這個屬性將變成<img>標記中的src屬性。
PleaseWaitType
PleaseWaitTypeEnum枚舉值之一:TextOnly,ImageOnly,TextThenImage,或ImageThenText。它控制訊息和圖片的佈局。
下面是.aspx檔案範例,它示範了一個設定了PleaseWaitText和PleaseWaitImage的PleastWaitButton。
<%@ Page language="C#" %>
<%@ Register TagPrefix="cc1" Namespace="JavaScriptControls"
Assembly="PleaseWaitButton" %>
<script runat="server">
private void PleaseWaitButton1_Click(object sender, System.EventArgs e)
{
// Server-side Click event handler;
// simulate something that could take a long time,
// like a file upload or time-consuming server processing
DateTime dt = DateTime.Now.AddSeconds(5);
while (DateTime.Now < dt)
{
// do nothing; simulate a 5-second pause
}
// at the end of the loop display a success message
// and hide the submit form
panelSuccess.Visible = true;
PleaseWaitButton1.Visible = false;
}
</script>
<html>
<head>
<title>Testing PleaseWaitButton</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<P>Testing the PleaseWaitButton control.</p>
<cc1:PleaseWaitButton id="PleaseWaitButton1" runat="server"
Text="Click me to start a time-consuming process"
PleaseWaitText="Please Wait "
PleaseWaitImage="pleaseWait.gif"
OnClick="PleaseWaitButton1_Click" />
<asp:Panel id="panelSuccess" runat="server"
visible="false">
Thank you for submitting this form. You are truly
the coolest user I've ever had the pleasure of serving.
No, really, I mean it. There have been others, sure,
but you are really in a class by yourself.
</asp:Panel>
</form>
</body>
</html>
How It Works
PleaseWaitButton控制項在<div>標記中呈現了一個標準的asp.net Button。它也呈現了一個空的<div>標記給資訊/圖像。在點擊按鈕時,由Javascript函數(請參閱下面的客戶端函數)控制按鈕的隱藏和資訊的顯示。為了方便起見,由PleaseWaitButton伺服器控制項處理所有必需的javascript客戶端程式碼的實作。
由於PleaseWaitButton實作它自己的javascript onclick句柄,所以我們必須採取一些額外的措施來保持原有的onclick句柄,並且允許控制項清晰地運行一些客戶端驗證程式碼。為了達到這個目的,我們先把Button基底類別還原為一個字串緩衝,然後巧妙地處理它,把我們定義的onclick程式碼包含進去。
protected override void Render(HtmlTextWriter output)
{
// Output the button's html (with attributes)
// to a dummy HtmlTextWriter
StringWriter sw = new StringWriter();
HtmlTextWriter wr = new HtmlTextWriter(sw);
base.Render(wr);
string sButtonHtml = sw.ToString();
wr.Close();
sw.Close();
// now modify the code to include an "onclick" handler
// with our PleaseWait() function called appropriately
// after any client-side validation.
sButtonHtml = ModifyJavaScriptOnClick(sButtonHtml);
// before rendering the button, output an empty <div>
// that will be populated client-side via javascript
// with a "please wait" message"
output.Write(string.Format("<div id='pleaseWaitButtonDiv2_{0}'>",
this.ClientID));
output.Write("</div>");
// render the button in an encapsulating <div> tag of its own
output.Write(string.Format("<div id='pleaseWaitButtonDiv_{0}'>",
this.ClientID));
output.Write(sButtonHtml);
output.Write("</div>");
}
這種把button還原成一個字串緩衝然後處理它的onclick內容的技術是一件很危險的事情(is certainly a hack). 但它可以讓我們在父button類別中實作標準的驗證程式碼,然後再實作我們的PleaseWait() Javascript函數呼叫。如果不這樣做,我們只能在驗證程式碼之前就在onclick屬性中實作我們的PleaseWait()函數調用,除非我們願意完全重寫父Button類別的屬性的呈現。這樣就算頁面上有輸入錯誤也會產生我們不想要的按鈕隱藏和顯示"please wait"訊息的效果。因此,我們必須在onclick句柄中強行令我們的客戶端PleaseWait()函數出現在客戶端頁面驗證之後。
onclick屬性的修改發生在ModifyJavaScriptOnClick()函數中。這個函數取得按鈕呈現的HTML字串,並檢查看是否存在onclick屬性。如果是,這個函數會檢查是否有使用客戶端驗證程式碼。如果是這種情況的話,我們定義的PleaseWait()函數會加在已經存在的onclick程式碼的最後面,緊跟著在客戶端檢查的boolin變數Page_IsValid後面。這個變數代表是否使用了驗證控制項。如果Page_IsValid的值是false,"Please wait"資訊將不會顯示。如果為True則顯示。
private string ModifyJavaScriptOnClick(string sHtml)
{
// Thanks to CodeProject member KJELLSJ (Kjell-Sverre Jerijaervi)
// for code ideas to allow the button to work with client-side validation
string sReturn = "";
string sPleaseWaitCode = GeneratePleaseWaitJavascript();
// is there an existing onclick attribute?
Regex rOnclick = new Regex("onclick="(?<onclick>[^"]*)");
Match mOnclick = rOnclick.Match(sHtml);
if (mOnclick.Success)
{
// there is an existing onclick attribute;
// add our code to the end of it; if client-side
// validation has been rendered, make sure
// we check to see if the page is valid;
string sExisting = mOnclick.Groups["onclick"].Value;
string sReplace = sExisting
+ (sExisting.Trim().EndsWith(";") ? "" : "; ");
if (IsValidatorIncludeScript() && this.CausesValidation)
{
// include code to check if the page is valid
string sCode = "if (Page_IsValid) " + sPleaseWaitCode
+ " return Page_IsValid;";
// add our code to the end of the existing onclick code;
sReplace = sReplace + sCode;
}
else
{
// don't worry about the page being valid;
sReplace = sReplace + sPleaseWaitCode;
}
// now substitute our onclick code
sReplace = "onclick="" + sReplace;
sReturn = rOnclick.Replace(sHtml, sReplace);
}
else
{
// there isn't an existing onclick attribute;
// add ours
int i = sHtml.Trim().Length - 2;
string sInsert = " onclick="" + sPleaseWaitCode + "" ";
sReturn = sHtml.Insert(i, sInsert);
}
return sReturn;
}
這個IsValidatorIncludeScript() 利用上面的檢查來查看是否有使用頁面註冊的asp.net驗證控制項的標準Javascript程式碼區塊。下面則用一個簡單的方法測試了是否有驗證程式碼和像Page_IsValid的變數存在。
private bool IsValidatorIncludeScript()
{
// return TRUE if this page has registered javascript
// for client-side validation; this code may not be registered
// if ASP.NET detects what it thinks (correctly or incorrectly)
// is a down-level browser.
return this.Page.IsStartupScriptRegistered("ValidatorIncludeScript");
} 下面這個GeneratePleaseWaitJavascript()建構了包含在onclick屬性中的PleaseWait() Javascript函數。我們可以透過檢查控制項的屬性來決定想要的佈局。
private string GeneratePleaseWaitJavascript()
{
// create a JavaScript "PleaseWait()" function call
// suitable for use in an onclick event handler
string sMessage = "";
string sText = _pleaseWaitText;
string sImage = (_pleaseWaitImage != String.Empty
? string.Format(
"<img src="{0}" align="absmiddle" alt="{1}"/>"
, _pleaseWaitImage, _pleaseWaitText )
: String.Empty);
// establish the layout based on PleaseWaitType
switch (_pleaseWaitType)
{
case PleaseWaitTypeEnum.TextThenImage:
sMessage = sText + sImage;
break;
case PleaseWaitTypeEnum.ImageThenText:
sMessage = sImage + sText;
break;
case PleaseWaitTypeEnum.TextOnly:
sMessage = sText;
break;
case PleaseWaitTypeEnum.ImageOnly:
sMessage = sImage;
break;
}
// return the final code chunk
string sCode = string.Format(
"PleaseWait('pleaseWaitButtonDiv_{0}',
'pleaseWaitButtonDiv2_{1}', '{2}');"
, this.ClientID, this.ClientID, sMessage);
sCode = sCode.Replace(""", """);
return sCode;
}
如果指定了一個PleaseWaitImage,就必須包含額外的一段Javascript程式碼來通知客戶端預載該映像。這段腳本的註冊應該會出現在重寫的OnPreRender方法中。註冊的鍵是映像的名稱;如果多個按鈕都使用相同映像,預載腳本只需要實施一次。這裡使用了一個正規表示式來建立Javascript圖像變量,以確保特殊字字元(例如檔案路徑中的斜線)轉換成下劃線。
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
// If we're using an image, register some javascript
// for client-side image preloading
if (_pleaseWaitImage != String.Empty
&& _pleaseWaitType != PleaseWaitTypeEnum.TextOnly)
RegisterJavascriptPreloadImage(_pleaseWaitImage);
}
private void RegisterJavascriptPreloadImage(string sImage)
{
Regex rex = new Regex("[^a-zA-Z0-9]");
string sImgName = "img_" + rex.Replace(sImage, "_");
StringBuilder sb = new StringBuilder();
sb.Append("<script language='JavaScript'>");
sb.Append("if (document.images) { ");
sb.AppendFormat("{0} = new Image();", sImgName);
sb.AppendFormat("{0}.src = "{1}";", sImgName, sImage);
sb.Append(" } ");
sb.Append("</script>");
this.Page.RegisterClientScriptBlock(sImgName + "_PreloadScript",
sb.ToString());
}
Client-side functions
嵌入的文字檔案javascript.txt包含了隱藏按鈕的<div>和顯示"please wait"訊息或圖像的客戶端程式碼。這些程式碼在重寫的OnInit()方法中呼叫的私有方法RegisterJavascriptFromResource()載入。這個方法呼叫泛型方法GetEmbeddedTextFile() ,在這個泛型方法中把檔案做為來源載入而把內容傳回成字串。
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// the client-side javascript code is kept
// in an embedded resource; load the script
// and register it with the page.
RegisterJavascriptFromResource();
}
private void RegisterJavascriptFromResource()
{
// load the embedded text file "javascript.txt"
// and register its contents as client-side script
string sScript = GetEmbeddedTextFile("javascript.txt");
this.Page.RegisterClientScriptBlock("PleaseWaitButtonScript", sScript);
}
private string GetEmbeddedTextFile(string sTextFile)
{
// generic function for retrieving the contents
// of an embedded text file resource as a string
// we'll get the executing assembly, and derive
// the namespace using the first type in the assembly
Assembly a = Assembly.GetExecutingAssembly();
String sNamespace = a.GetTypes()[0].Namespace;
// with the assembly and namespace, we'll get the
// embedded resource as a stream
Stream s = a.GetManifestResourceStream(
string.Format("{0}.{1}", sNamespace, sTextFile)
);
// read the contents of the stream into a string
StreamReader sr = new StreamReader(s);
String sContents = sr.ReadToEnd();
sr.Close();
s.Close();
return sContents;
}
javascript.txt嵌入資源包含了按鈕在Javascript的onclick句柄中執行的客戶端方法PleaseWait()。這段程式碼也呼叫了一個客戶端方法HideDiv()以隱藏按鈕的容器<div>,然後透過設定innerHTML屬性把資訊或映像組裝進之前空的<div>標記中。輔助函數GetDiv()則是透過檢查document.getElementById, document.all, 和document.layers用id傳回一個<div>對象,確保了不同瀏覽器的相容性。下面是javascript.txt的全部程式碼:
<script language="JavaScript">
function GetDiv(sDiv)
{
var div;
if (document.getElementById)
div = document.getElementById(sDiv);
else if (document.all)
div = eval("window." + sDiv);
else if (document.layers)
div = document.layers[sDiv];
else
div = null;
return div;
}
function HideDiv(sDiv)
{
d = GetDiv(sDiv);
if (d)
{
if (document.layers) d.visibility = "hide";
else d.style.visibility = "hidden";
}
}
function PleaseWait(sDivButton, sDivMessage, sInnerHtml)
{
HideDiv(sDivButton);
var d = GetDiv(sDivMessage);
if (d) d.innerHTML = sInnerHtml;
}
</script>
原文連結: http://www.codeproject.com/aspnet/PleaseWaitButton.asp
Download Source Project - 7 Kb
Download Demo Project - 30 Kb
http://www.cnblogs.com/jeffamy/archive/2006/08/20/481952.html