protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "this.value='提交中…';" + disableString());
//Button1.Attributes["onclick"] = Page.ClientScript.GetPostBackEventReference(Button1, String.Empty) + ";this.disabled=true;";
}
/// <summary>
/// 返回禁用所有按钮的js代码
/// </summary>
/// <returns></returns>
private string disableString()
{
StringBuilder tempStr = new StringBuilder();
tempStr.Append(@"if (document.all||document.getElementById)");
tempStr.Append(@"{");
tempStr.Append(@"var currentForm = document.forms[0];");
tempStr.Append(@"for (i=0;i<currentForm.length;i++)");
tempStr.Append(@"{");
tempStr.Append(@"var tempobj=currentForm.elements[i];");
tempStr.Append(@"if(tempobj.type.toLowerCase()=='button' || tempobj.type.toLowerCase()=='text')");
tempStr.Append(@"{");
tempStr.Append(@"tempobj.disabled=true;");
tempStr.Append(@"}");
tempStr.Append(@"}");
tempStr.Append(@"}");
return tempStr.ToString();
} <asp:Button ID="Button1" UseSubmitBehavior="False" OnClientClick="return SumbitTxt()" runat="server" Text="Button" OnClick="Button1_Click" />
this.Button1.Attributes["onclick"] = "if(document.getElementById('TextBox1').value=='a'){setStyle('TextBox1');return false;}else{cancelStyle('TextBox1');"+Page.ClientScript.GetPostBackEventReference(this.Button1, null) + ";this.disabled=true;"+"}";//防止非法重复提交
本文介绍了一种在ASP.NET Web应用程序中防止按钮被重复点击的方法。通过在服务器端设置按钮的点击事件,并利用客户端JavaScript代码来禁用页面上所有的按钮,从而避免了由于网络延迟等原因导致的按钮重复提交问题。
955

被折叠的 条评论
为什么被折叠?



