简体   繁体   English

Javascript函数没有停止Button.Click ASP.Net代码

[英]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. 如果我尝试使用自定义Javascript函数,我似乎无法让OnClientClick不运行ASP.net代码。 However if I use just the standard "javascript:return confirm('whatever')" that will stop the submit if I click cancel. 但是,如果我只使用标准的“javascript:return confirm('whatever')”,如果我点击取消,将停止提交。 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: 这是处理按钮点击的VB.Net函数:

    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: 这是客户的Javascript功能:

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? 再次,为什么它会使用正常的返回confirm()停止onclick并单击取消,但是当我使用我的函数并单击取消时不会停止代码?

You need to put a return in the OnClientClick handler. 您需要在OnClientClick处理程序中放置一个return

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM