此处提供的代码用来实现当asp.net页面中的某个button被点击后disable掉该页面中所有的button,从而防止提交延时导致的多次提交。基于之前的onceclickbutton脚本.
<script language=“javascript“>
<!–
function disableothersubmit()
{ var obj = event.srcelement;
var objs = document.getelementsbytagname(input);
for(var i=0; i<objs.length; i++)
{
if(objs[i].type.tolowercase() == submit)
{
objs[i].disabled = true;
}
} }
//–>
</script>
<!–
function disableothersubmit()
{ var obj = event.srcelement;
var objs = document.getelementsbytagname(input);
for(var i=0; i<objs.length; i++)
{
if(objs[i].type.tolowercase() == submit)
{
objs[i].disabled = true;
}
} }
//–>
</script>
public class preventmulticlick : system.web.ui.page
{
protected system.web.ui.webcontrols.button button1;
protected system.web.ui.webcontrols.button button2;
protected system.web.ui.webcontrols.linkbutton linkbutton1;
protected system.web.ui.webcontrols.button button3;
private void page_load(object sender, system.eventargs e)
{
this.getpostbackeventreference(this.button3); //保证 __dopostback(eventtarget, eventargument) 正确注册
if(!ispostback)
{
system.text.stringbuilder sb = new system.text.stringbuilder();
sb.append(“if (typeof(page_clientvalidate) == function) { if (page_clientvalidate() == false) { return false; }}“); //保证验证函数的执行
sb.append(“if(window.confirm(are you sure?)==false) return false;“); //自定义客户端脚本
sb.append(“disableothersubmit();“); // disable所有submit按钮
sb.append(this.getpostbackeventreference(this.button3)); //用__dopostback来提交,保证按钮的服务器端click事件执行
sb.append(“;“);
button3.attributes.add(“onclick“,sb.tostring());
}
}
override protected void oninit(eventargs e)
{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// required method for designer support – do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.button3.click += new system.eventhandler(this.button3_click);
this.load += new system.eventhandler(this.page_load);
{
system.threading.thread.sleep(3000);
response.write(“hello world!“);
}
}
{
protected system.web.ui.webcontrols.button button1;
protected system.web.ui.webcontrols.button button2;
protected system.web.ui.webcontrols.linkbutton linkbutton1;
protected system.web.ui.webcontrols.button button3;
private void page_load(object sender, system.eventargs e)
{
this.getpostbackeventreference(this.button3); //保证 __dopostback(eventtarget, eventargument) 正确注册
if(!ispostback)
{
system.text.stringbuilder sb = new system.text.stringbuilder();
sb.append(“if (typeof(page_clientvalidate) == function) { if (page_clientvalidate() == false) { return false; }}“); //保证验证函数的执行
sb.append(“if(window.confirm(are you sure?)==false) return false;“); //自定义客户端脚本
sb.append(“disableothersubmit();“); // disable所有submit按钮
sb.append(this.getpostbackeventreference(this.button3)); //用__dopostback来提交,保证按钮的服务器端click事件执行
sb.append(“;“);
button3.attributes.add(“onclick“,sb.tostring());
}
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// required method for designer support – do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.button3.click += new system.eventhandler(this.button3_click);
this.load += new system.eventhandler(this.page_load);
}
private void button3_click(object sender, system.eventargs e)
{
system.threading.thread.sleep(3000);
response.write(“hello world!“);
}
}
此处只是disable掉所有的submit button, 我觉得其它的可提交控件也是可以通过类似的方法来disable的.