javascript 防止重复提交
ASP.NET利用JavaScript防止重复提交
Button1.Attributes["onclick"] = this.GetPostBackEventReference(this.Button1) + ";this.disabled=true;";
Javascript
很多时候我们都需要防止重复提交,这方面的文章也比较多,实现的途径差别也很大.以下是我写的一种控制提交的方式.因为有些时候即使服务器能够识别重复的提交,也会造成问题.比如需要很长等待时间的操作,在首次提交后,不断重复提交,页面可能会死掉.用脚本来控制的话可以防止这种问题.当然也可以脚本和服务器都进行控制,这样就比较完美了.
闲话少说,将以下脚本放置于页面顶部.
1
document.IsPosted = false;
2
function CancelDubSubmit()
3
{
4
if ((typeof(event.returnValue) == "undefined" || event.returnValue== true) && !document.IsPosted)
5
{
6
document.IsPosted = true;
7
event.returnValue = true;
8
}
9
else
10
{
11
event.returnValue = false;
12
}
13
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
以下加粗部分放置于form标签中,如果你已经有了onsubmit事件的其他执行函数,可以放在一起,最好将CancelDubSubmit()函数放在最后.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
其中document.IsPosted是为了记录是否回送.一旦页面回送,document.IsPosted将为true. 重新加载后,document.IsPosted=false将被执行.当onsubmit事件没有其他执行函数或者其他执行函数返回true并且document.IsPosted=false时,回送页面,否则停止回送.
以上方法不能控制使用 function __doPostBack() 函数的服务器端控件.因为在此函数中的提交是靠 document.Form1.submit() 实现的,它不会触发Onsubmit事件.那么我们还需要重写__doPostBack() 函数.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
请将以上代码放置于页面的原__doPostBack() 函数之后.