如何在第一次单击后禁用按钮

Ish*_*han 11 javascript c# asp.net buttonclick

我有一个按钮的onclick事件.一旦这个事件被ftonired我想要禁用该按钮.谁能帮我理解怎么做?

这是我在buttonclick事件上执行的代码.

protected void Button3_Click(object sender, EventArgs e)
{

    if (Session["login"] != null && Session["db"] != null)
    {

        digit b = new digit();
        String digitformed = b.formdigit(this.DropDownList1, this.TextBox1, this.TextBox2);

        chekcount c = new chekcount();
        int count = c.getmaxcountforjud_no(digitformed);
        int addtocount = count + 1;

        String name = Session["login"].ToString();
        String databs = Session["db"].ToString();
        String complex_name = name + databs;

        if (DropDownList2.SelectedItem.Text != "New")
        {
            update u = new update();
            u.update1(this.Editor1, digitformed, this.TextBox3, complex_name, name, this.DropDownList2);
            Response.Write(@"<script language='javascript'>alert('Updated')</script>");

        }
        else
        {
            save d = new save();
            d.dosave(this.Editor1, addtocount, digitformed, this.TextBox3, complex_name, name);
            Response.Write(@"<script language='javascript'>alert('Saved')</script>");

        }
    }
    else
    {
        Response.Redirect("log.aspx");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我要禁用的按钮.

<asp:Button ID="Button3" runat="server" Text="Save" onclick="Button3_Click" 
                    Visible="False" />
Run Code Online (Sandbox Code Playgroud)

Pra*_*lla 29

使用按钮控件的OnClientClick和UseSubmitBehavior属性.

<asp:Button runat="server" ID="BtnSubmit" 
  OnClientClick="this.disabled = true; this.value = 'Submit in progress...';" 
  UseSubmitBehavior="false" 
  OnClick="BtnSubmit_Click" 
  Text="Click to Submit" />
Run Code Online (Sandbox Code Playgroud)

OnClientClick允许您添加客户端OnClick脚本.在这种情况下,JavaScript将禁用button元素并将其文本值更改为progress消息.回发完成后,新呈现的页面将使按钮恢复其初始状态,而无需任何额外的工作.

在客户端禁用提交按钮带来的一个缺陷是它将取消浏览器的提交,从而取消回发.将UseSubmitBehavior属性设置为false会使.NET注入必要的客户端脚本以终止回发,而不是依赖于浏览器的表单提交行为.在这种情况下,它注入的代码将是:

__doPostBack('BtnSubmit','')
Run Code Online (Sandbox Code Playgroud)

Redered HTML:

<input type="button" name="BtnSubmit" 
  onclick="this.disabled = true; this.value = 'Submitting...';__doPostBack('BtnSubmit','')"
  value="Submit Me!" id="BtnSubmit" />
Run Code Online (Sandbox Code Playgroud)

这应该给你想要的行为.

来自:http ://encosia.com/disable-a-button-control-during-postback/图片来源:Dave Ward(Twitter:@Encosia)


Ren*_*iuz 16

你有没有尝试过?:

protected void Button3_Click(object sender, EventArgs e)
{
   Button3.Enabled = false;
   //rest of code
}
Run Code Online (Sandbox Code Playgroud)

  • 这不会禁用用户在浏览器中的按钮,他们仍然可以反复单击按钮不受禁止. (3认同)

Ima*_*idi 5

<asp:Button  onclick="Button3_Click" ID="Button3" runat="server" Text="Save"
OnClientClick="this.disabled = true; this.value = 'please wait ..';" 
UseSubmitBehavior="false"     />
Run Code Online (Sandbox Code Playgroud)


Dav*_*ave 5

您需要在服务器端代码中设置按钮的Enabled属性,如其他海报所详述.但是,如果您尝试阻止来自同一按钮的多个提交,则需要稍微不同的方法.

向您的班级添加方法:

static void DisableButtonDuringPostback(Page page, Button control)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("this.disabled = true;");
    sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString()));
    sb.Append(";");

    control.Attributes.Add("onclick", sb.ToString());

}
Run Code Online (Sandbox Code Playgroud)

在Page_Load中添加

DisableButtonDuringPostback(this.Page, Button3);
Run Code Online (Sandbox Code Playgroud)