Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a button after 1st click

I have the following onclick event for a button. once this event is ftonired i want to disable the button. Can anyone help me to understand how to do it?

Here is the code i execute on buttonclick event.

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");
    }
}

Here is the Button which i want to disable.

<asp:Button ID="Button3" runat="server" Text="Save" onclick="Button3_Click" 
                    Visible="False" />
like image 703
Ishan Avatar asked Dec 12 '11 11:12

Ishan


2 Answers

Use the OnClientClick and UseSubmitBehavior properties of the button control.

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

OnClientClick allows you to add client side OnClick script. In this case, the JavaScript will disable the button element and change its text value to a progress message. When the postback completes, the newly rendered page will revert the button back its initial state without any additional work.

The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser’s submit, and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser’s form submission behavior. In this case, the code it injects would be:

__doPostBack('BtnSubmit','')

Redered HTML:

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

This should give you the desired behavior.

From: http://encosia.com/disable-a-button-control-during-postback/ Credit: Dave Ward (Twitter: @Encosia)

like image 136
Prafulla Avatar answered Sep 28 '22 02:09

Prafulla


Have you tried?:

protected void Button3_Click(object sender, EventArgs e)
{
   Button3.Enabled = false;
   //rest of code
}
like image 41
Renatas M. Avatar answered Sep 28 '22 00:09

Renatas M.