How to execute a JS function before the CodeBehind method call? [duplicate]

1

I have a button asp

<asp:button ID="cmdAvancarEndereco" runat="server" 
   cssclass="btn btn-success btn-lg"   text="Avançar >">
</asp:button>

which has an event of VB linked in click

Private Sub cmdAvancarEndereco_Click(sender As Object, e As EventArgs) 
                                      Handles cmdAvancarEndereco.Click

'evento

End Sub

But in the click of this button, I need to execute before the event cmdAvancarEndereco_Click a javascript event:

var getCardToken = function () {
// evento
}

I tried to put this without success it goes straight to pro server:

document.getElementById("ContentPlaceHolder1_cmdAvancarEndereco")
        .addEventListener("click", getCardToken);'

Any solution?

    
asked by anonymous 10.02.2017 / 15:13

1 answer

1

Add the OnClientClick attribute with the return of your function (must return true or false), if true, it calls the event in CodeBehind.

<asp:button ID="cmdAvancarEndereco" runat="server" cssclass="btn btn-success btn-lg" text="Avançar >" OnClientClick="return getCardToken()"></asp:button>
    
10.02.2017 / 15:24