How to execute a JS function before the so-called Codebehind method?

Asked

Viewed 68 times

1

I have an Asp button

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

that has an event of the VB trailer in the click

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

'evento

End Sub

but at the click of this button, I need to run before the event cmdAvancarEndereco_Click a javascript event:

var getCardToken = function () {
// evento
}

tried to put it unsuccessfully it will go straight to the server:

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

Some solution?

  • Put Onclientclick on the button and call the function! Here in this mine question You can see how the button looks!

  • Try this and see if it solves.. http://answall.com/a/181725/64073

  • Face unfortunately I do not see as duplicate, because the question there has not to do with how to execute first and yes which to execute first and no solution was given equal here, the questions are different

1 answer

1


Add the attribute Onclientclick with the return of its function (must return true or false), if return 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>
  • is it possible to make more than one function call ? example : <asp:button ID="cmdAvancarEndereco" runat="server" cssclass="btn btn-success btn-lg" text="Avançar >" OnClientClick="return getCardToken(); teste();"></asp:button>

  • 1

    Yes, it’s possible, you could do: if(primeiraFuncao) { return segundaFuncao(); }

  • Great thanks so much !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.