返回禁用所有按钮的js代码

本文介绍了一种在ASP.NET Web应用程序中防止按钮被重复点击的方法。通过在服务器端设置按钮的点击事件,并利用客户端JavaScript代码来禁用页面上所有的按钮,从而避免了由于网络延迟等原因导致的按钮重复提交问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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;"+"}";//防止非法重复提交



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值