user3334102
user3334102

Reputation: 3

Javascript Function Not Stopping Button.Click ASP.Net Code

I have been frustrated today with what seems like an easy piece of code. I cannot seem to get the OnClientClick to not run the ASP.net code if I try to use a custom Javascript function. However if I use just the standard "javascript:return confirm('whatever')" that will stop the submit if I click cancel. Can you help me to understand why my custom function is not working to do the same thing?

Here is the code for the button:

<asp:Button ID="ButtonSubmitChanges" runat="server" Text="Submit Changes" Class="justround" autofocus="autofocus" 
    OnClientClick="javascript:myCustomConfirm()"/> 

Here is the VB.Net Function that handles the button click:

    Protected Sub ButtonSubmitChanges_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSubmitChanges.Click

    Dim ChangeErrorMessage As String = SubmitChanges()

    If ChangeErrorMessage.Length <= 0 Then

        Response.Redirect("ViewChangeRequest.aspx?CRID=" & ChangeRequest_ID.ToString)
    Else
        labelSystemMessage.Text = ChangeErrorMessage
    End If
End Sub

And here is the customer Javascript function:

function myCustomConfirm() {

return confirm("Are you sure?");
}

Again, why will it stop the onclick with the normal return confirm() and click Cancel, but not stop the code when I use my function and click cancel?

Upvotes: 0

Views: 604

Answers (1)

Aric TenEyck
Aric TenEyck

Reputation: 8032

You need to put a return in the OnClientClick handler.

<asp:Button ID="ButtonSubmitChanges" runat="server" 
Text="Submit Changes" Class="justround" autofocus="autofocus"    
OnClientClick="javascript:return myCustomConfirm()"/> 

Upvotes: 2

Related Questions