0

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?

3
  • It's been a while, but I believe you need to use AutoWireup="false" in your .aspx. Commented Feb 20, 2014 at 18:29
  • 1
    You're not returning the value returned by "myCustomConfirm()". You're just calling it and throwing away the result. Commented Feb 20, 2014 at 18:30
  • Oh my goodness, it was so simple that I want to bash my head against the keyboard. Thank you @Pointy I knew it seemed too difficult.... Commented Feb 20, 2014 at 18:39

1 Answer 1

2

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()"/> 
Sign up to request clarification or add additional context in comments.

1 Comment

Geez, I knew it was something simple, but now I just feel ridiculous. Thank you so much @AricTenEyck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.