JavaScript disable button

Posted on 2013-01-28
Medium Priority
Last Modified: 2013-01-29
How would I go about disabling a .Net button on Click event using JavaScript?
Question by:lrbrister
LVL 41

Accepted Solution

guru_sami earned 1400 total points
ID: 38829316
There are several ways. Here's one:
function Disable(btn) {
            btn.disabled = true;
<asp:Button ID="btnSave" runat="server" Text="Button" OnClientClick="Disable(this);" />

Open in new window

LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 600 total points
ID: 38829400
The above will disable the button. If you want to prevent the button from running server-side code then you can use this:

<asp:Button ID="btnSave" runat="server" Text="Button" OnClientClick="return false;"></asp:button>

Open in new window

You can augment this system by returning the value of any function. I've found this handy in the past. For example, you could write a javascript function which asks the user for confirmation via the "confirm" javascript method. If they don't confirm then return false. It might look like this:

function foo()
    if (!confirm("Are you sure?"))
        return false;
    return true;

<asp:Button ID="btnSave" runat="server" Text="Button" OnClientClick="return foo();"></asp:button>

Open in new window

LVL 10

Expert Comment

by:Monica P
ID: 38829920
<asp:Button ID="Button1" runat="server" Text="Button"  onclientclick="disableBtn"/>

<script type="text/javascript">
    function disableBtn()
    var btn= document.getElementById("<%=Button1.ClientID%>");

get the button id

Var button1=document.getElementById('<%= btnName.ClientID %>')



or disable like this

btnName.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnName).ToString());

incase of restricting multiple click on postback

LVL 10

Expert Comment

by:Monica P
ID: 38829923

Author Closing Comment

ID: 38831027
You guys were first.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In a question here at Experts Exchange, a member was looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray". This article shows how to achieve that, as well as providing the same OF…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question