Disable on button click

Discussion in 'ASP .Net' started by Barry Gilmore, Oct 17, 2005.

  1. 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!
    Barry Gilmore, Oct 17, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:roQ4f.469$-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!
    >
    >
    Ken Dopierala Jr., Oct 17, 2005
    #2
    1. Advertising

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

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


    "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!
    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 17, 2005
    #3
  4. 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" <> wrote in message
    news:roQ4f.469$-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!
    >
    >
    S. Justin Gengo, Oct 17, 2005
    #4
  5. 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." <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:roQ4f.469$-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!
    >>
    >>

    >
    >
    S. Justin Gengo, Oct 17, 2005
    #5
  6. 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" <> wrote in message
    news:roQ4f.469$-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!
    >
    >
    Bishoy George, Oct 18, 2005
    #6
  7. 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
    JIMCO Software, Oct 18, 2005
    #7
  8. "JIMCO Software" <> wrote in message
    news:...
    > 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
    Bishoy George, Oct 18, 2005
    #8
  9. 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
    JIMCO Software, Oct 18, 2005
    #9
  10. 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" <> wrote in message
    news:roQ4f.469$-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!
    >
    >
    Bishoy George, Oct 18, 2005
    #10
  11. 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" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:roQ4f.469$-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!
    > >
    > >

    >
    >
    Ken Dopierala Jr., Oct 18, 2005
    #11
  12. 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." <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:%...
    >> 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" <> wrote in message
    >> news:roQ4f.469$-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!
    >> >
    >> >

    >>
    >>

    >
    >
    Bishoy George, Oct 19, 2005
    #12
  13. 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." <> wrote in message
    > news:%...
    > > 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" <> wrote in message
    > > news:%...
    > >> 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" <> wrote in message
    > >> news:roQ4f.469$-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!
    > >> >
    > >> >
    > >>
    > >>

    > >
    > >

    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 19, 2005
    #13
  14. 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
    JIMCO Software, Oct 20, 2005
    #14
  15. 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
    >
    >
    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 20, 2005
    #15
  16. 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" <> wrote in message
    news:u4%...
    > 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
    >
    >
    >
    >
    S. Justin Gengo, Oct 21, 2005
    #16
  17. 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
    JIMCO Software, Oct 21, 2005
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    802
    Purvi T
    Oct 19, 2004
  2. Sinity

    Disable button after first click

    Sinity, Nov 12, 2003, in forum: ASP .Net
    Replies:
    15
    Views:
    15,940
    nayak
    Sep 4, 2007
  3. Sinity

    Disable button after first click

    Sinity, Nov 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    403
    Sinity
    Nov 12, 2003
  4. nicholas

    disable button on click

    nicholas, Dec 5, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    8,100
    nicholas
    Dec 8, 2003
  5. Amy
    Replies:
    0
    Views:
    943
Loading...

Share This Page