troubleshooting Question

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

Avatar of purplesoup
purplesoupFlag for United Kingdom of Great Britain and Northern Ireland asked on
ASP.NETC#JavaScriptjQuery
2 Comments1 Solution143 ViewsLast Modified:
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>

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);
        }
    }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros