views:

49

answers:

1

Am trying to re-use a function i got from one of the threads on this site. I want to disable an asp.net button when clicked such that a user does not click it several times.

The button is inside an Ajax Updatepanel and also has a ConfirmButtonExtender, but when i click it, it get this javascript error in internet explorer.

**

Error: 'return' statement outside of function

**

When i remove the ConfirmButtonExtender, every thing works fine.

Below is the function

 Private Function DisableTheButton(pge As Control, btn As Control) As String
    Dim sb As New System.Text.StringBuilder()
    sb.Append("if (typeof(Page_ClientValidate) == 'function') {")
    sb.Append("if (Page_ClientValidate() == false) { return false; }} ")
    sb.Append("if (confirm('Are you sure to proceed?') == false) { return false; } ")
    sb.Append("this.value = 'Please wait...';")
    sb.Append("this.disabled = true;")
    sb.Append(Page.ClientScript.GetPostBackEventReference(btn, ""))
    sb.Append(";")
    Return sb.ToString()
End Function

And below is my page load

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.btnSubmit.Attributes.Add("onclick", DisableTheButton(Me.Page, Me.btnSubmit))
End Sub

Am also using the asp.net validation controls the button.

+1  A: 

Wrap your JS-Code into an anonymous function and passed it the button

sb.Append("(function(btn) {")
sb.Append("if (typeof(Page_ClientValidate) === "function" && confirm("Are you sure to proceed?")) {")
sb.Append("btn.value = 'Please wait...';")
sb.Append("btn.disabled = true;")
sb.Append("Page.ClientScript.GetPostBackEventReference(btn, ""))")
sb.Append("}})(this);")
john_doe