How to turn off submit button when it pressed in ASP.Net

Category > ASP.NET || Published on : Monday, January 11, 2016 || Views: 10719 || turn off submit button when it pressed in ASP.Net turn off submit button


Introduction

Here Pawan Kumar will explain how to turn off submit button when it pressed in ASP.Net

Description

In previous post I have explained Set DropDownList value based on text/value in jQuery, How to change some text before it is sent to the client in asp.net with example, How to sum the value of gridview column using jquery in asp.net with example, How to compress response in asp.net with example, How to display progress indicator during ajax call in asp.net using jQuery, How to rotating image with ASP.NET 4.0 and C# with example, and many more articles.

Now I will explain How to How to turn off submit button when it pressed in ASP.Net

So follow the steps to learn How to turn off submit button when it pressed in ASP.Net

In some conditions, we need to turn off submit button when user pressed in ASP.NET so to prevent the double action.
Take an example of paymnet gateway, We have to disable the "confirm" button to avoid duplicate payment transaction when user click the button once.

The following example will solve this problem adn it will turn it off the submit button adn show message when it press using Server Side Code

Step 1: Create a ASP.NET website in Visual Studio 2010

Step2: Create a ASPX webpage and add a button with name "btnConfirmPayment" on webpage

        <asp:Button ID="btnConfirmPayment" runat="server" OnClick="Button1_Click" 
            Text="Confirm Payment" Width="152px" />

Step 3: Add the following code to code behind of webpage created in last step. This will turn off submit button when user pressed in ASP.NET.
you have to write it on "OnLoad" page event

this.btnConfirmPayment.Attributes.Add("onclick", "this.value='Please wait...';this.disabled = true;"
            + Page.ClientScript.GetPostBackEventReference(this.btnConfirmPayment, ""));

Now, click the button every times, it disable itself and user don't have a chance to press the button double.

Complete code for ASPX page:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to turn off submit button when it pressed in ASP.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnConfirmPayment" runat="server" OnClick="Button1_Click" 
            Text="Confirm Payment" Width="152px" />
    </div>
    </form>
</body>
</html>


Complete code for ASPX page code behind:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        this.btnConfirmPayment.Attributes.Add("onclick", "this.value='Please wait...';this.disabled = true;"
            + Page.ClientScript.GetPostBackEventReference(this.btnConfirmPayment, ""));
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

Screenshot:

 

Conclusion:

So, In this tutorial we have learned, How to turn off submit button when it pressed in ASP.Net

Download Source Codes