0

I'm working in C# and I need a button to become instantly disabled when a user clicks it. However, if I put as the very first line in the OnClick function

MyButton.Enabled = false;

it does nothing. The button remains enabled until it hits some sort of stop, whether it be the Catch block or the end of the function.

I was thinking that in VB/VBA you can use DoEvents() and that allows the code to catch up to itself. However, in C# there doesn't appear to be a DoEvents method.

This is a little different than the linked question, in that my OnClick code looks like:

OnClick="btnSubmit_Click"

and that user's OnClick code looks like:

onClick="this.disabled=true;
this.value='Sending…';
this.form.submit();"

When I tried to change my code to:

OnClick="this.disabled=true; btnSubmit_Click"

I got an error.

Compiler Error Message: CS1041: Identifier expected; 'this' is a keyword

How can I do this in a C#/asp.net environment?

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
  • 3
    You need to [use JavaScript](http://stackoverflow.com/questions/3366828/how-to-disable-submit-button-once-it-has-been-clicked). – Liam May 12 '15 at 13:41
  • You could use jquery for these client-side behavior. – L-Four May 12 '15 at 13:42
  • 3
    You seem to confuse winforms with client-server technologies like ASP.NET. The button gets clicked on client-side, that's why you can't disable it from serverside. – Tim Schmelter May 12 '15 at 13:46
  • 1
    Technically you can disable it from the server side but it will be **after** the postback has occurred, which is why you see a delay. The client side code(JavaScript) is ran **before** the postback so it will be instantaneous. – Liam May 12 '15 at 13:50
  • That linked answer shows something like, OnClick = "this.disabled=true". So, how woul dI do that if OnClick is already set to run a function? It gave me an error when I changed it to, OnClick = "this.disabled=true; btnSubmit_Click" – Johnny Bones May 12 '15 at 13:59

2 Answers2

4

OnClick is a server-side event of the Button. So you cannot write:

OnClick="this.disabled=true; btnSubmit_Click"

OnClick accepts only the method-name of the server-side event handler.

If you want to handle the client-side button-click event to prevent that the user can click on it multiple times use OnCLientClick:

OnCLientClick = "this.disabled=true;"

You also have to set UseSubmitBehaviour to false.

Read: Disable a button control during postback.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
-4

You will need to utilize client-side JavaScript. One way is to utilize the OnClientClick property of your asp.net button control.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick(v=vs.110).aspx

This is server-side and will be executed on PageLoad/Postback -

MyButton.Enabled = false;
Kris Krause
  • 7,304
  • 2
  • 23
  • 26
  • 1
    This isn't a great answer. You should really explain how to use OnClientClick, what it is...something! As it stands it's more of a comment than an answer – Liam May 12 '15 at 13:43
  • @KrisKrause: the last line suggests that this does what OP wants: disable the button, so that the user cannot click multiple times on it. But it doesn't do that since that affects only the html that **will be sent** to the client. Until then the user can still click the buttton. I guess OP doesn't want to disable the button completely but only until the page is proecssed. – Tim Schmelter May 12 '15 at 14:09
  • "the last line suggests that this does what OP wants" no it doesn't –  May 12 '15 at 14:29
  • @Dust: i don't understand your comment. I just tried to explain why others might downvoted the answer and said that this is the wrong approach after Kris has edited it. The part that was edited starts with "This is server-side..." and basically repeats what OP has already tried. It's the wrong approach because it doesn't work. – Tim Schmelter May 12 '15 at 14:44
  • How is Kris' answer wrong approach? It is actually pretty good because it links the correct client-side event page where the OP can read and get familiar with it instead of just copy past code. –  May 12 '15 at 14:55
  • @Dust: the downvotes came after he edited the answer and added the last part which describes how to disable a button on serverside. That is not what OP wants and he already shows it in his question if you look at the first part. He wants to prevent double clicks which causes to postback again. That cannot be achieved by disabling the button on serverside. – Tim Schmelter May 12 '15 at 15:02