We help IT Professionals succeed at work.
Get Started

How to dynamically disable buttons on a page after sending back to server?

purplesoup
purplesoup asked
on
137 Views
Last Modified: 2017-11-28
I've seen example code for submitting a form, but I want to disable all the buttons on my web form after it is sent back to the server - my attempt is below but it isn't working.

aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestForDisableButtonsOnPage.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <hr />
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
            <asp:Button ID="Button3" runat="server" Text="Button" OnClick="Button3_Click" />
            <asp:Button ID="Button4" runat="server" Text="Button" OnClick="Button4_Click" />
        </div>
    </form>
    
<script type="text/javascript">
    $(":button").keyup(function () {
        $(":button").prop('disabled', true);
    });
 
</script>

</body>
</html>

Open in new window


C#
using System;
using System.Threading;

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

        protected void Button1_Click(object sender, EventArgs e)
        {
            ButtonClick(1);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ButtonClick(2);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            ButtonClick(3);
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            ButtonClick(4);

        }

        private void ButtonClick(int i)
        {
            Label1.Text = $"Button {i} was clicked";
            Thread.Sleep(5000);
        }
    }
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE