ASP.NET服务器控件PleaseWaitButton[翻译] (2)
点击次数:20 次 发布日期:2008-11-26 12:25:06 作者:源代码网
|
源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 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 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
