Disable submit button after user click

Discussion in 'ASP .Net' started by GS, Jan 19, 2006.

  1. GS

    GS Guest

    Hi,

    This is probably known solution but I have not found it in minimum amount of code.
    I'd like to disable button on client-side after clicking on it.
    Just disabling it in javascript do not produce postback event.
    Any examples in ASP.NET 2.0?

    Thanks,
     
    GS, Jan 19, 2006
    #1
    1. Advertisements

  2. GS

    Peter Zolja Guest

    This is probably known solution but I have not found it in minimum amount
    Don't know about 2.0, but this works in 1.1, give it a try:

    1. in the PageLoad event

    if (!IsPostBack)
    {
    ibSomeButton.Attributes.Add("onclick",
    "this.style.display='none';");
    // other stuff
    }

    The "ibSomeButton" is an ImageButton. However, this will not prevent the
    user from hitting refresh which can generate a postback.
     
    Peter Zolja, Jan 19, 2006
    #2
    1. Advertisements

  3. GS

    tdavisjr Guest

    This will hide the button and not disable it. But the goad is still
    acheived. May this will work:

    idSomeButton.Attributes.Add("onclick", "this.disabled= 'disabled' ");
     
    tdavisjr, Jan 19, 2006
    #3
  4. GS

    Peter Zolja Guest

    This will hide the button and not disable it. But the goad is still
    That's correct, but form my experience users don't notice the disabled
    state... and still try to click it... so what I do is hide the button and
    unhide some text that tells them to wait...
     
    Peter Zolja, Jan 19, 2006
    #4
  5. GS

    GS Guest

    Thanks,

    But I wanted to use Button not ImageButton.
    I actuallty found the piece of code to do it, the only problem is that
    Page.GetPostBackEventReference is obsolete and I'm supposed to use some new
    ASP.NET 2.0 method which I have not figured out how.

    btnGetImportInfo.Attributes["onclick"] =
    "this.disabled=true;this.value='Please wait...';" +
    Page.GetPostBackEventReference(btnGetImportInfo ).ToString();
     
    GS, Jan 19, 2006
    #5
  6. GS

    drflower Guest

    This will hide the button and not disable it. But the goad is still
    I have wanted to do this for a long time, however using a method such
    as this one does not work (or at least, did not work for me in v1.1)
    because if the page was invalid, the onclick would disable the button,
    but the form would not be submitted due to the invalid data.

    For example, imagine there is a required field called name, which the
    user leaves blank and presses submit. The submit button gets disabled,
    and then the user is asked to enter their name. Problem.

    Or am I missing something?

    Daniel.
     
    drflower, Jan 19, 2006
    #6
  7. GS

    Peter Zolja Guest

    Or am I missing something?

    If you do the field checking on the client side then yes, you have a
    problem -- which can be solved by checking first and only if successful
    disable/hide the button. If, however, you allow a postback and do the
    checking on the server side everything should be fine since after a postback
    the page comes back with the button enabled/visible -- the default value...
     
    Peter Zolja, Jan 19, 2006
    #7
  8. GS

    Peter Zolja Guest

    But I wanted to use Button not ImageButton.

    I image it's the same... haven't tried though... (my code was using an
    ImageButton and I just took it from there)
    Sorry, I'm still in the 1.1 land. Untested code, try this:

    btnGetImportInfo.Attributes.Add("onclick",
    "this.disabled=true;this.value='Please wait...';");
     
    Peter Zolja, Jan 19, 2006
    #8
  9. GS

    GS Guest

    Code below will not work since disabling button happens before postback and
    hence no postback happens when you disable button.

     
    GS, Jan 19, 2006
    #9
  10. GS

    DisableButton

    Joined:
    May 4, 2007
    Messages:
    1
    Likes Received:
    0
    Disable the Button, Then Manually Submit the Form

    Try this:

    <input type="submit" id="Submit" name="Submit" value="Submit" onclick="javascript:document.form1.Submit.disabled=1; document.form1.submit();">
     
    DisableButton, May 4, 2007
    #10
  11. GS

    bryian

    Joined:
    Aug 16, 2009
    Messages:
    1
    Likes Received:
    0
    check this out too -
    ht tp://blog.ysatech.com/post/2009/08/13/Disable-an-ASPNET-button-during-PostBack-with-AJAX-loading-background-image.aspx
     
    bryian, Aug 16, 2009
    #11
  12. GS

    W0ut

    Joined:
    May 14, 2011
    Messages:
    2
    Likes Received:
    0
    W0ut, May 14, 2011
    #12
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.