Which event runs first, javascript or Asp.net?

Asked

Viewed 127 times

1

I have a javascript method on a button, which is triggered with the click of it, but this same button triggers an Asp.net event. The js event executes a method that processes a value and this value I play within a Hidden (Asp). The Asp event uses this value (from Hidden) for another process. My problem is that it seems that the Asp event is processing before JS so when Asp tries to get the value generated by JS (which should be in Hidden), the value is empty.

Any idea how to run js first in this case? or any other idea?

document
      .getElementById("ContentPlaceHolder1_cmdAvancarEnder‌​eco")
      .addEventListen‌​er("click", getHashSender); 

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

<asp:button ID="cmdAvancarEndereco" runat="server" 
                  cssclass="btn btn-success btn-lg" text="Avançar >">
</asp:button>
  • What is your code?

  • Document.getElementById("Contentplaceholder1_cmdavancarendereco"). addeventlistener("click", getHashSender); <- JS

  • ContentPlaceHolder1_cmdAvancarEnder‌​eco is a button submit ?

  • Guy sorry that’s the code : Document.getElementById("Contentplaceholder1_cmdavancarendereco"). addeventlistener("click", getHashSender); <- JS Private Sub cmdAvancarEndereco_Click(Sender As Object, and As Eventargs) Handles cmdAvancarEndereco.Click 'event end sub the forward knob and a knob Asp <Asp:button ID="cmdAvancarEndereco" runat="server" cssclass="btn btn-Success btn-lg" text="Next >"></Asp:button> &#Xa)

  • just click on the editor where the keys are { } the text needs to be selected. In your case the Button requests the server first, javascript does not even execute, I believe. It’s kind of without context but, that would be it.

  • but I say here in the comments, or there is no way?

  • has yes is com dois acentos crase

Show 2 more comments

2 answers

1


The solution found was in the event onClickof the button inform a return false; thus barring the event of codebehind, and only when my Hidden is filled, is the event forced submit form to process the data (in case I could force the event click, but the solution has changed).

1

You can take control of the .cs and do it just for .js.

Transforms the method Private Sub cmdAvancarEndereco_Click(sender As Object, e As EventArgs) in a normal method Public Sub cmdAvancarEndereco_Click({ parametros_necessarios }). Hence the code asp will be called by your javascript after being sued by him.

document.getElementById("ContentPlaceHolder1_cmdAvancarEnder‌​eco").addEventListen‌​er("click", function(evt) {
   callPostBack(null, this, [urlcmdAvancarEndereco_Click], [parametros]);
}); 

This ensures that before arriving at the code backend will go through .js.

  • guy sorry but what does this urlcmdAvancarEndereco_Click mean would be the name of my page / method? in my case would be cart/cmdAvancarEndereco_Click , ? or I’m wrong?

  • var url = '@Url.Action("NomeAction", "NomeController", null, Request.Url.Scheme)'; I use this structure to dynamically assemble my url.. in case it would be var urlcmdAvancarEndereco_Click= '@Url.Action("cmdAvancarEndereco_Click", "Nome_Controller", null, Request.Url.Scheme)';

  • then face the problem and that I’m using webforms

  • got.. well, see if this link can help.. http://stackoverflow.com/questions/9654808/do-something-javascript-before-asp-net-anypostback

Browser other questions tagged

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