Disable on button click

B

Barry Gilmore

Is there a way to disable a button after it is clicked?

I am trying to avoid having someone click on it twice while they wait for it
to process.

Thank you!
 
K

Ken Dopierala Jr.

Hi Barry,

This should work:

Button1.Attribute.Add("onclick", "this.disabled = true;
document.Form1.submit(); return false;")

I didn't test it but I know you need to submit the form yourself and cancle
the event bubble on the submit button. Good luck! Ken.
 
G

Guest

adding following code in the onClick event (javascript), will stop the user
from pressing it again

document.getElementById('buttonname').disabled='true';
 
S

S. Justin Gengo

Barry,

I have a javascript that first makes a call to .net's built in
authentication scripts to make certain a form is submittable (you wouldn't
want to disable a button if the clientside script decides it's not valid and
doesn't submit thus stranding the user) and then disables a second click of
said button.

The code is available as part of a Javascript component that is free for
download from my website as a Visual Studio.Net 2003 project. Even if you
don't want to use the whole component (it has other scripts such as open a
centered popup window, click a button when the enter key is pressed while in
a text box, scroll to an element on the page, etc.) you could always strip
out the code you want. All the components on my site are free, all come with
complete source code, and there is a help file you may download if you'd
like.

You may download the component from here:
http://www.aboutfortunate.com?page=javascript

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

S. Justin Gengo

Ken,

That will work, but what if the form isn't valid. Now the user can't fix and
resubmit...


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
B

Bishoy George

Why using javascript?
It can be done very simply by C# code...

private void Button1_Click(object sender, System.EventArgs e)
{
Button1.Enabled = false;
}

Bishoy
 
J

JIMCO Software

Bishoy said:
Why using javascript?
It can be done very simply by C# code...

private void Button1_Click(object sender, System.EventArgs e)
{
Button1.Enabled = false;
}

I think what he's trying to do is prevent someone from submitting a form
twice. Doing that in server-side code wouldn't not work.
 
B

Bishoy George

JIMCO Software said:
I think what he's trying to do is prevent someone from submitting a form
twice. Doing that in server-side code wouldn't not work.
--------------------------------------------------------------

It is working with me.
I think if you revised what Mr.Barry Gilmore wanted, you may find my code
efficient.

Bishoy
http://bishoy.com
 
J

JIMCO Software

Bishoy said:
It is working with me.
I think if you revised what Mr.Barry Gilmore wanted, you may find my
code efficient.

Bishoy,

If you set the Enabled property of the button in server-side code, it only
affects the button when the page is rendered AFTER the postback. That won't
prevent someone from clicking the Submit button twice on a form. In order
to do that, you need to disable the button on the client immediately after
the form is submitted but before it actually POSTs.

Your code will not meet that need.
 
B

Bishoy George

I tried the code submitted by Ken Dopierala Jr.
It disable the button for a second then when the page is refreshed due to
post back, it is enabled again.

Also the code of Sreejith Ram is not working.

Bishoy
http://bishoy.com
 
K

Ken Dopierala Jr.

Hi Bishoy,

That is what it is supposed to do. Let's say you are doing a credit card
payment. It makes sure the user can only click once. Then after processing
is done they are redirected to a success or failure page. It also lets the
user know that they can only click the button once. On your site I can get
in over a dozen clicks before the page comes back to alert me. This also
means I posted 10 requests to your server instead of just one. If you were
doing a database app that required a few seconds to process and has multiple
steps it will really get hammered and you'll need to rollback 9 transactions
that may have started before the first transaction completed. While you
should have the logic to rollback those transactions in your app it is best
to do what you can to make sure you only need to executed a minimum number
of times. Ken.
 
B

Bishoy George

Hi Ken,
Your opinion is very reasonable.

But, I want an extra code to prevent the disabled button by adding
attributed from become active again.

Thanks.
Bishoy

------------------------------------------------------------------------
 
G

Guest

Well Bishoy, I think the issue we trying to address is , if the user click
multiple times on submit button, the client browser would send multiple post
requests to the web server. This happens a lot when the client connection is
slow. Our code doesnt have any control on this because the browser would
think these are 2 diffrent posts by the user.

The simplest solution for it is to stop the user from clicking multiple
times by disabling the button at clientside after the first click. .so that
multiple requests are not send to the server by the browser. Like Justin
Gengo pointed this wouldnt work if you have validators though ..

Once you are at the Server Side OnClick event, you would be able to take the
additional step of disabling the button , clearing form data, redirecting the
page etc etc depending on the requirement of your app. But if you disable the
submit button for good, how will the user make the next submit?
 
J

JIMCO Software

Sreejith said:
The simplest solution for it is to stop the user from clicking
multiple times by disabling the button at clientside after the first
click. .so that multiple requests are not send to the server by the
browser. Like Justin Gengo pointed this wouldnt work if you have
validators though ..

The following will work even if you have validation controls:

System.Text.StringBuilder sb = new StringBuilder();
sb.Append("if (typeof(Page_ClientValidate) == 'function') { ");
sb.Append("if (Page_ClientValidate() == false) { return false; }} ");
sb.Append("this.value = 'Please wait...';");
sb.Append("this.disabled = true;");
sb.Append(Page.GetPostBackEventReference(btnSubmit));
sb.Append(";");
btnSubmit.Attributes.Add("onclick", sb.ToString());
 
G

Guest

thanks

JIMCO Software said:
The following will work even if you have validation controls:

System.Text.StringBuilder sb = new StringBuilder();
sb.Append("if (typeof(Page_ClientValidate) == 'function') { ");
sb.Append("if (Page_ClientValidate() == false) { return false; }} ");
sb.Append("this.value = 'Please wait...';");
sb.Append("this.disabled = true;");
sb.Append(Page.GetPostBackEventReference(btnSubmit));
sb.Append(";");
btnSubmit.Attributes.Add("onclick", sb.ToString());
 
S

S. Justin Gengo

Jimco,

Yes, that's what my disable submit function (submitted above) does.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
J

JIMCO Software

S. Justin Gengo said:
Jimco,

Yes, that's what my disable submit function (submitted above) does.

Sorry if reposted. I posted in response to a "doesn't work with validators"
post. I hide read posts, do unless I happen to remember a previous post's
content, it's gone clear into another realm by the time I might respond. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top