欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET 多次提交的解决办法2

程序员文章站 2022-05-22 13:44:04
例如: protected void page_load(object sender, eventargs e) { //.net1.1 button1.attribute...
例如:
protected void page_load(object sender, eventargs e)
{
//.net1.1
button1.attributes.add("onclick", "this.disabled=true;" + this.getpostbackeventreference(this.button1));
//.net 2.0以上
button1.attributes.add("onclick", "this.disabled=true;" + this.clientscript.getpostbackeventreference(button1, ""));
}
为了测试,我们可以建立一个页面,加入一个btnadd按钮
<%@ page language="c#" autoeventwireup="true" codefile="addandedituser.aspx.cs" inherits="adduser" %><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>添加和编辑页面示例</title>
</head>
<body>
<form id="frmmain" runat="server">
<asp:button id="btnadd" runat="server" cssclass="input-button-save" onclick="btnadd_click">
</asp:button>
</form>
</body>
</html>
/*----------------------------------------------------------------
using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
public partial class adduser : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
btnadd.attributes.add("onclick", "this.disabled=true;" + this.clientscript.getpostbackeventreference(btnadd, ""));
}
protected void btnadd_click(object sender, eventargs e)
{
//模拟网络拥塞5秒钟
system.threading.thread.sleep(5000);
}
}
可见当点击了按钮,需要等5秒钟按钮才能点击。即页面回发一后按钮才能用。这样可以有效的防止用户多次点击按钮,造成多次提交!