Results 1 to 7 of 7

Thread: Disabling Submit Button

  1. #1

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Disabling Submit Button

    I'd like to disable a submit button to prevent mutiple database entries.
    I've not had a problem disabling the button using Javascript but the method i used seems to intercept the postback and so the normal code is never executed. I found out i had to manually post the form, with this the form is submitted and page_load runs but i still can't get the button_onclick event to run after the button is disabled. code.....
    VB Code:
    1. 'In code behind
    2. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.     If Not IsPostBack Then
    4.         btnSubmit.Attributes.Add("onclick", "return disableMe(this);")
    5.     End If
    6. End Sub
    7.  
    8. Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
    9.     'Fairly long process here
    10. End Sub
    11.  
    12. 'In Script in .aspx file
    13.         <script language="javascript">
    14.             function disableMe(btn) {
    15.                 btn.disabled = true;
    16.                 document.form1.submit();
    17.                 return true;
    18.             }
    19.         </script>
    I've found quite a few examples on the web but couldn't get any of them to work.

    any ideas?

  2. #2

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    anyone? I'd really like to get this working if at all possible.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    So you just want to disable a Submit button after it has been pressed (sent back to the server)?

    Then in your code behind, just set the SubmitButton.Enabled property to false in your click_event.

    Then you don't need the javascript.

    But, as far as the Javascript, instead of
    Code:
     document.form.submit
    you have to something like this
    Code:
    __dopostback('x');
    I forget what x is though.. I think its the id of the control that raised the postback event.
    Last edited by nemaroller; Apr 16th, 2004 at 10:09 PM.

  4. #4

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    The problem with setting enabled = false is that until the code behind the button has finished the user can keep clicking on the button over and over until the page is rendered again with the button disabled.

    Cheers, i'll look at calling _dopostback instead of submiting th form.

    Thanks.

  5. #5

  6. #6

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    Thanks for those links, just what i wanted.

  7. #7

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    Finally got this working exactly as i wanted. Stops a button begin reclicked until processing from the first instance has finished. This code also works when there are validators on the page. Here's the code incase it's useful to anyone else.

    VB Code:
    1. Dim sb As New System.Text.StringBuilder
    2.         sb.Append("if (typeof(Page_ClientValidate) == 'function') { ")
    3.         sb.Append("if (Page_ClientValidate() == false) { return false; }} ")
    4.         sb.Append("this.value = 'Please wait...';")
    5.         sb.Append("this.disabled = true;")
    6.         sb.Append(GetPostBackEventReference(btnSubmit))
    7.         sb.Append(";")
    8.  
    9.         If Not IsPostBack Then
    10.             btnSubmit.Attributes.Add("onclick", sb.ToString())
    11.         End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width