471,305 Members | 1,272 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Disable on button click

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!
Nov 19 '05 #1
16 3252
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Barry Gilmore" <no**********@yahoo.com> wrote in message
news:ro**************@tornado.rdc-kc.rr.com...
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!

Nov 19 '05 #2
adding following code in the onClick event (javascript), will stop the user
from pressing it again

document.getElementById('buttonname').disabled='tr ue';
"Barry Gilmore" wrote:
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!

Nov 19 '05 #3
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
"Barry Gilmore" <no**********@yahoo.com> wrote in message
news:ro**************@tornado.rdc-kc.rr.com...
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!

Nov 19 '05 #4
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
"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:uF*************@tk2msftngp13.phx.gbl...
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Barry Gilmore" <no**********@yahoo.com> wrote in message
news:ro**************@tornado.rdc-kc.rr.com...
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!


Nov 19 '05 #5
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
-------

"Barry Gilmore" <no**********@yahoo.com> wrote in message
news:ro**************@tornado.rdc-kc.rr.com...
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!

Nov 19 '05 #6
Bishoy George wrote:
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.

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com


Nov 19 '05 #7
"JIMCO Software" <co*******@jimcosoftware.com> wrote in message
news:uP**************@TK2MSFTNGP14.phx.gbl...
Bishoy George wrote:
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.

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com

--------------------------------------------------------------

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
Nov 19 '05 #8
Bishoy George wrote:

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.

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com


Nov 19 '05 #9
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
-----------------------------------------------------

"Barry Gilmore" <no**********@yahoo.com> wrote in message
news:ro**************@tornado.rdc-kc.rr.com...
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!

Nov 19 '05 #10
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bishoy George" <bi****@bishoy.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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
-----------------------------------------------------

"Barry Gilmore" <no**********@yahoo.com> wrote in message
news:ro**************@tornado.rdc-kc.rr.com...
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!


Nov 19 '05 #11
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

------------------------------------------------------------------------

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bishoy George" <bi****@bishoy.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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
-----------------------------------------------------

"Barry Gilmore" <no**********@yahoo.com> wrote in message
news:ro**************@tornado.rdc-kc.rr.com...
> 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!
>
>



Nov 19 '05 #12
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?

"Bishoy George" wrote:
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

------------------------------------------------------------------------

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bishoy George" <bi****@bishoy.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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
-----------------------------------------------------

"Barry Gilmore" <no**********@yahoo.com> wrote in message
news:ro**************@tornado.rdc-kc.rr.com...
> 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!
>
>



Nov 19 '05 #13
Sreejith Ram wrote:

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());

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com


Nov 19 '05 #14
thanks

"JIMCO Software" wrote:
Sreejith Ram wrote:

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());

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com


Nov 19 '05 #15
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
"JIMCO Software" <co*******@jimcosoftware.com> wrote in message
news:u4****************@tk2msftngp13.phx.gbl...
Sreejith Ram wrote:

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());

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com

Nov 19 '05 #16
S. Justin Gengo wrote:
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. ;)

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com


Nov 19 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by nntp | last post: by
14 posts views Thread by Sinity | last post: by
3 posts views Thread by Jeff | last post: by
reply views Thread by Robert Ladd | last post: by
4 posts views Thread by Buddy Ackerman | last post: by
reply views Thread by Ahmad Jalil Qarshi | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.