asp.net表單提交時(shí)防重復(fù)提交并執(zhí)行前臺(tái)的JS驗(yàn)證
更新時(shí)間:2013年10月25日 17:14:21 作者:
今天遇到這樣的一個(gè)情況,就是用戶重復(fù)提交。當(dāng)然這個(gè)不能怪用戶,只能怪.NET或者服務(wù)器反應(yīng)遲鈍,下面有個(gè)不錯(cuò)的教程,大家可以參考下
在項(xiàng)目開發(fā)中,遇到這樣的一個(gè)情況,就是用戶重復(fù)提交。當(dāng)然這個(gè)不能怪用戶,只能怪.NET或者服務(wù)器反應(yīng)遲鈍......我是這樣理解的。
在網(wǎng)上搜了一下,解決方案是不少,比如:
http://bbs.csdn.net/topics/340048988
(這個(gè)大家提了不少建議)
http://www.cnblogs.com/blsong/archive/2009/12/24/1631144.html
(這個(gè)基本上總結(jié)了網(wǎng)上的方法)
但實(shí)際上做互聯(lián)網(wǎng)web項(xiàng)目中,需要在前臺(tái)執(zhí)行JS或者Jquery的驗(yàn)證(主要是增強(qiáng)用戶體驗(yàn)),那么再使用上面的方法,就會(huì)出現(xiàn)問題。要么重復(fù)提交依然存在,要么前臺(tái)JS驗(yàn)證失效。最后沒辦法,只有自己寫一個(gè),在滿足阻止用戶重復(fù)提交的情況下,還能保證前臺(tái)JS驗(yàn)證有效。代碼如下:
//按鈕注冊(cè)加載樣式事件
var ItSelfButton;
var ControlRegPostResult = true;
function AddInputClick() {
$("input[type='submit']").click(function () {
ItSelfButton = $(this);
if (ItSelfButton.attr("repeat") == null) {
var btnDiv = $("<div>");
btnDiv.attr("id", "Mask_BTN");
var divimg = $("<img>");
divimg.attr("alt", "加載中...");
divimg.attr("src", "/Images/ButtonLoading.gif");
divimg.css({ "margin-left": ($(this).width() - 4) / 2, "margin-top": ($(this).height() - 16) / 2 });
btnDiv.append(divimg);
btnDiv.css({ width: $(this).width() + 12 + "px", height: $(this).height() + "px", top: $(this).offset().top + "px", left: $(this).offset().left + "px", position: "absolute" });
$(document.body).append(btnDiv);
setTimeout(MaskTimeOutRemove, 200);
}
});
}
$(function () {
AddInputClick();
});
$(window).resize(function () {
if (ItSelfButton != null) {
$("#Mask_BTN").css({ top: ItSelfButton.offset().top + "px", left: ItSelfButton.offset().left + "px" });
}
});
function MaskRemove() {
$("#Mask_BTN").remove();
}
function MaskTimeOutRemove() {
if (!ControlRegPostResult) {
$("#Mask_BTN").remove();
ControlRegPostResult = true;
}
}
其中在JS 驗(yàn)證失敗中將
ControlRegPostResult = false;
這樣基本上滿足我的目的了。
ButtonLoading.gif 可以是一個(gè)打轉(zhuǎn)的圖片 ,也可以和按鈕一樣大。反正目的是這個(gè)層把按鈕遮住。
在網(wǎng)上搜了一下,解決方案是不少,比如:
http://bbs.csdn.net/topics/340048988
(這個(gè)大家提了不少建議)
http://www.cnblogs.com/blsong/archive/2009/12/24/1631144.html
(這個(gè)基本上總結(jié)了網(wǎng)上的方法)
但實(shí)際上做互聯(lián)網(wǎng)web項(xiàng)目中,需要在前臺(tái)執(zhí)行JS或者Jquery的驗(yàn)證(主要是增強(qiáng)用戶體驗(yàn)),那么再使用上面的方法,就會(huì)出現(xiàn)問題。要么重復(fù)提交依然存在,要么前臺(tái)JS驗(yàn)證失效。最后沒辦法,只有自己寫一個(gè),在滿足阻止用戶重復(fù)提交的情況下,還能保證前臺(tái)JS驗(yàn)證有效。代碼如下:
復(fù)制代碼 代碼如下:
//按鈕注冊(cè)加載樣式事件
var ItSelfButton;
var ControlRegPostResult = true;
function AddInputClick() {
$("input[type='submit']").click(function () {
ItSelfButton = $(this);
if (ItSelfButton.attr("repeat") == null) {
var btnDiv = $("<div>");
btnDiv.attr("id", "Mask_BTN");
var divimg = $("<img>");
divimg.attr("alt", "加載中...");
divimg.attr("src", "/Images/ButtonLoading.gif");
divimg.css({ "margin-left": ($(this).width() - 4) / 2, "margin-top": ($(this).height() - 16) / 2 });
btnDiv.append(divimg);
btnDiv.css({ width: $(this).width() + 12 + "px", height: $(this).height() + "px", top: $(this).offset().top + "px", left: $(this).offset().left + "px", position: "absolute" });
$(document.body).append(btnDiv);
setTimeout(MaskTimeOutRemove, 200);
}
});
}
$(function () {
AddInputClick();
});
$(window).resize(function () {
if (ItSelfButton != null) {
$("#Mask_BTN").css({ top: ItSelfButton.offset().top + "px", left: ItSelfButton.offset().left + "px" });
}
});
function MaskRemove() {
$("#Mask_BTN").remove();
}
function MaskTimeOutRemove() {
if (!ControlRegPostResult) {
$("#Mask_BTN").remove();
ControlRegPostResult = true;
}
}
其中在JS 驗(yàn)證失敗中將
復(fù)制代碼 代碼如下:
ControlRegPostResult = false;
這樣基本上滿足我的目的了。
ButtonLoading.gif 可以是一個(gè)打轉(zhuǎn)的圖片 ,也可以和按鈕一樣大。反正目的是這個(gè)層把按鈕遮住。
相關(guān)文章
ASP.NET中控件的EnableViewState屬性及徹底禁用
如果我們?cè)陂_發(fā)Web應(yīng)用程序時(shí),某些控件是不需要接受用戶的操作或只需要接受一次操作的時(shí)候,我們可以將這些控件的EnableViewState屬性改為false,這樣可以優(yōu)化我們的程序,提高網(wǎng)絡(luò)訪問的速度。2016-06-06asp.net調(diào)用系統(tǒng)設(shè)置字體文本框的方法
這篇文章主要介紹了asp.net調(diào)用系統(tǒng)設(shè)置字體文本框的方法,包括設(shè)置文本字體樣式和大小,需要的朋友可以參考下2014-09-09ASP.NET使用gridview獲取當(dāng)前行的索引值
這篇文章主要介紹了ASP.NET使用gridview獲取當(dāng)前行的索引值的方法匯總,有需要的小伙伴可以參考下。2015-06-06