Solved

JavaScript disable button

Posted on 2013-01-28
5
283 Views
Last Modified: 2013-01-29
How would I go about disabling a .Net button on Click event using JavaScript?
0
Comment
Question by:lrbrister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 350 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

0
 
LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 150 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

0
 
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%>");
    btn.disabled=true;
    }
</script>



get the button id

Var button1=document.getElementById('<%= btnName.ClientID %>')
button1.style.display="block";

or

document.getElementById("button1").style.display="block";

or disable like this

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

incase of restricting multiple click on postback

http://encosia.com/disable-a-button-control-during-postback/
0
 
LVL 10

Expert Comment

by:Monica P
ID: 38829923
0
 

Author Closing Comment

by:lrbrister
ID: 38831027
You guys were first.
Thanks
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help with this homework problem. 5 55
Tools for analysis of code 4 44
selected value 16 24
Html date format 9 20
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

761 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