预防"提交"按钮的多次点击
Google,找到了一些代码,自己修改了一下。主要参考老猫'blog.
publicvoidpage_load(Objectobj,EventArgse)
{
btn.Attributes.Add("onclick","state=true;");
StringBuildersb=newStringBuilder();
sb.Append("if(!state)return;");
sb.Append("varbutton=document.getElementById('btn');");
sb.Append("button.value=\"PleaseWait...\";");
sb.Append("document.body.style.cursor='wait';");
sb.Append("button.disabled=true;");
stringstrScript="<script>";
strScript=strScript+"varstate=false;";
//将函数绑定到页面的onbeforeunload事件:
strScript=strScript+"window.attachEvent('onbeforeunload',function(){"+sb.ToString()+"});";
strScript=strScript+"</"+"script>";
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "onbeforeunload", strScript);
}
privatevoidSubmit_Click(Objectsender,EventArgse){
//模拟长时间的按钮处理
System.Threading.Thread.Sleep(3000);
Response.Write("<script>alert(xxx!!');"+"</"+"script>");
}
<asp:buttonid="btn"Text="Submit"OnClick="Submit_Click"runat="server"/>
修改好的这个例子原理是提交后将按钮置于一定时间的无效状态.
仅能防止一些,因网速慢而心急的用户造成的误点击.
对于恶意的多次刷新而形成的多次提交还是没有办法解决.
(不过应该可通过页面的onbeforeunload事件解决)
如何在用户关掉web浏览器窗口前,进行相应的动作 参考maison 'blog | |
} 触发于:
可以用在以下元素: |