All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET,1.1,无,Cookie,SessionID,重写_[Asp.Net教程]
Linux系统Apache用户授权和访问控制_php资料_编程技术
asp.net计算网站访问量常用代码_[Asp.Net教程]
Delphi使用ODBC连接SQServer 2000数据库
ASP.NET程序实现三色交替的下拉列表框_[Asp.Net教程]
sql2005,用户sa,登录失败,该用户与可信SQL,Server连接无关联_mssql学习_编程技术
区别和认识.Net四个判等函数_.net资料_编程技术
用ImessageFilter实现截获键盘动作_[Asp.Net教程]
不妨来做个尝试:UpdatePanel,for,ASP.NET,MVC_[Asp.Net教程]
WEB标准时页面自适应的解决方案_[Html教程]
Reading number is top 10 pictures
The world's ten biggest attractions of inventory super the moon
XuYing poker perspective garment debut
两张抽象画
The household of rural style is designed
NeedWallpaper13
BingBingFan apple dew point photo gallery5
八个盛产美女的国家2
Ashlynn Brooke photograph of a group1
Hunan road lawenforcement vehicle pursue overload car caused the car turn and man is died
西游日记1
Download software ranking
Tram sex maniac 2 (H) rar bag10
Tram sex maniac 2 (H) rar bag8
Sora aoi's film--cangkong_Blue.Sky
asp.net技术内幕
WebService在.NET中的实战应用教学视频 → 第5集
Sora aoi‘s film--Lust fan wall
c#程序设计案例教程
Red cliff
Jinling thirteen stock
Unix video tutorial13
delv published in(发表于) 2014/1/10 6:28:25 Edit(编辑)
ASP.NET服务器控件PleaseWaitButton_[Asp.Net教程]

ASP.NET服务器控件PleaseWaitButton_[Asp.Net教程]

ASP.NET服务器控件PleaseWaitButton_[Asp.Net教程]























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动画图像),如果存在,当按钮被点击它将取代按钮。这个属性将变成标记中的src属性。
PleaseWaitType
  PleaseWaitTypeEnum枚举值之一:TextOnly,ImageOnly,TextThenImage,或者ImageThenText。它控制消息和图片的布局。




  下面是一个.aspx文件示例,它演示了一个设置了PleaseWaitText和PleaseWaitImage的PleastWaitButton。







<%@ Page language="C#" %>
<%@ Register TagPrefix="cc1" Namespace="JavaScriptControls"
Assembly="PleaseWaitButton" %>








Testing PleaseWaitButton




Testing the PleaseWaitButton control.



Text="Click me to start a time-consuming process"
PleaseWaitText="Please Wait "
PleaseWaitImage="pleaseWait.gif"
onClick="PleaseWaitButton1_Click" />

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.








How It Works




  PleaseWaitButton控件在

标记中呈现了一个标准的asp.net Button。它也呈现了一个空的

标记给
信息/图像。在点击按钮时,由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


// that will be populated client-side via javascript
// with a "please wait" message"
output.Write(string.Format("

",
this.ClientID));
output.Write("

");




// render the button in an encapsulating

tag of its own
output.Write(string.Format("

",
this.ClientID));
output.Write(sButtonHtml);
output.Write("

");
}
  这种把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(
"\"{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("\"", "&quot;");




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("");




this.Page.RegisterClientScriptBlock(sImgName + "_PreloadScript",
sb.ToString());
}




Client-side functions




  嵌入的文本文件javascript.txt包含了隐藏按钮的

和显示"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()以隐藏按钮的容器

,然后通过设置innerHTML属性把信息或图像组装进之前空的

标记中。辅助函数GetDiv()则是通过检查document.getElementById, document.all, 和 document.layers用id返回一个

对象,保证了不同浏览器的兼容性。下面是javascript.txt的全部代码:




原文链接:http://www.codeproject.com/aspnet/PleaseWaitButton.asp













































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.