在网速慢或者网站反应慢的情况下如果提交表单需要超过秒的时间还未提交成功多数人会重新点击提交这样不仅造成错误的数据还会加剧服务器的压力
通过使用HttpModule我们可以在表单处理前检测一些标志从而防止用户重复提交数据再通过一些接口让用户自己来处理重复提交时应该如何告诉用户
通过使用HttpModule我们也可以在客户端表单提交时使用DIV覆盖住表单从UI层防止用户再次单击提交(用户直接F管不了)
这种方法使用简单直接把脚本和图片放在指定的目录中然后在nfig中添加Module
<httpModules>
<!防止重复提交 LOG记录MODULE >
<add name=NonReduplicatePostModule type=testsNonReduplicatePostModuletest/>
</httpModules>
下面是实现代码
/// NonReduplicatePostModule 的摘要说明
/// </summary>
public class NonReduplicatePostModule : SystemWebIHttpModule
{
private static ILog log = LogManagerGetLogger(SystemReflectionMethodBaseGetCurrentMethod()DeclaringType);
private const string hiddenFileName = __NonReduplicatePostModule__;
private const string maskdivScriptRelativeUrl = ~/js/maskDivjs;
private const string onformSubmit = EvlonMaskDivInstanceshow();;
private HttpApplication context = null;
#region IHttpModule 成员
public void Init(HttpApplication context)
{
ntext = context;
ntextPreRequestHandlerExecute+=new EventHandler(context_PreRequestHandlerExecute);
}
public void Dispose()
{
ntextPreRequestHandlerExecute=new EventHandler(context_PreRequestHandlerExecute);
}
#endregion
private void context_PreRequestHandlerExecute(object sender EventArgs e)
{
HttpApplication webApp = sender as HttpApplication;
if(webApp != null)
{
//已经处理过提示用户不要重复提交
Page page = webAppContextHandler as Page;
if(page != null)
{
pagePreRender+=new EventHandler(page_PreRender);
//找到Page添加时间
if(webAppRequestForm[hiddenFileName] != null)
{
string flag = webAppRequestForm[hiddenFileName]ToString();
if(webAppContextCacheGet(flag) != null)
{
logDebug(found reduplicate post);
if(page is IReduplicatePostHandler)
{
webAppContextHandler = new ReduplicatePostHandler((IReduplicatePostHandler)page);
}
else
{
webAppContextHandler = new ReduplicatePostHandler();
}
}
else
{
//放进缓存中表示已经被处理过在一分钟后自动移聊(可再次提交)
webAppContextCacheAdd(flagDateTimeNownullSystemWebCachingCacheNoAbsoluteExpirationTimeSpanFromMinutes()SystemWebCachingCacheItemPriorityNormalnull);
}
}
}
}
}
private void page_PreRender(object sender EventArgs e)
{
Page page = sender as Page;
if(page != null)
{
//找到Page添加时间
pageRegisterHiddenField(hiddenFileNamestringFormat({}_{}_{}pageSessionSessionIDGetHashCode() pageGetType()GetHashCode() DateTimeNowTicks));
//表单UI显示MASKDIV
pageRegisterClientScriptBlock(maskdiv_include<script type=text/javascript src= + pageResolveUrl(maskdivScriptRelativeUrl) + ></script>);
pageRegisterOnSubmitStatement(maskdiv onformSubmit);
}
}
}
public interface IReduplicatePostHandler
{
void OnReduplicatePost(HttpContext context EventArgs e);
}
internal class ReduplicatePostHandler : IHttpHandler
{
private IReduplicatePostHandler handler = null;
internal ReduplicatePostHandler(IReduplicatePostHandler handler)
{
thishandler = handler;
}
internal ReduplicatePostHandler()
{
}
#region IHttpHandler 成员
public void ProcessRequest(HttpContext context)
{
if(handler != null)
{
handlerOnReduplicatePost(contextnew EventArgs());
}
else
{
contextResponseWrite(不要重复提交);
}
}
public bool IsReusable
{
get
{
// TODO: 添加 ReduplicatePostHandlerIsReusable getter 实现
return false;
}
}
#endregion
}
用到的JS文件:/js/MaskDIVjs
Evlon = {};
EvlonMaskDiv = function()
{
var div = windowdocumentcreateElement(DIV);
divstyleposition = absolute;
divstyletop = px;
divstyleleft = px;
divstylewidth = %;//documentbodyscrollWidth + px;
divstyleheight = %; //documentbodyscrollHeight + px;
divstylebackgroundColor = white;
divstylezIndex = ;
divstylefilter = Alpha(style=opacity=);
divstyleopacity=;
thisdivMask = div;
div = windowdocumentcreateElement(DIV);
divstyleposition = absolute;
divstyletop = px;
divstyleleft = px;
divstylewidth = %;//documentbodyscrollWidth + px;
divstyleheight = %; //documentbodyscrollHeight + px;
divstylezIndex = ;
thisdivTooltip = div;
thisshow = function()
{
//创建半透明DIV
window__maskDiv__ = documentbodyinsertAdjacentElement(afterBeginthisdivMask);
//创建提示DIV
window__maskDivText__ = documentbodyinsertAdjacentElement(afterBeginthisdivTooltip);
window__maskDivText__innerHTML = <table style=bordercollapse:collapse;borderwidth:px;width:%;height:%;><tr height=%><td></td></tr><tr><td align=center valign=top> +
<table style=bordercollapse:collapse;border:solid px yellow;><tr><td align=right><img width= height= src=image/loadinggif usesrc=image/loadinggif onerror=thissrc= thisusesrc = \/\ + thisusesrc/></td> +
<td align=left><span style = filter:alpha(opacity=);backgroundcolor:#ccc;fontsize:px>提交中</span></td></tr></table> +
</td></tr></table>;
}
thishide = function()
{
if(window__maskDiv__ != null)
{
documentbodyremoveChild(window__maskDiv__);
window__maskDiv__ = null;
}
if(window__maskDivText__ != null)
{
window__maskDivText__innerHTML = ;
documentbodyremoveChild(window__maskDivText__);
window__maskDivText__ = null;
}
}
}
EvlonMaskDivInstance = new EvlonMaskDiv();
windowshowModalDialogMask = function(sURL vArguments sFeatures)
{
var sign = windowshowModalDialog(sURL vArguments sFeatures);
if(sign != && sign != null)
{
//创建半透明DIV
EvlonMaskDivInstanceshow();
}
return null;
//return sign;
}