title: onclick and onclientclick webmethod executed after onclick
body: <p>I am trying to set a variable on using a webmethod (<code>.asmx</code>)</p>
  
  <p>The button is using <code>onclick</code> to call a server side method and <code>onclientclick</code> to call a javascript ajax which calls the webmethod.  </p>
  
  <p>I need the javascript code to be fully executed before the onclick server end method is called, but it seems as though the ajax call is not fully being executed before the postback is being made.  </p>
  
  <p>The alert is called before postback however the webmethod is being called after the postback.  I need the variable to be set from the webmethod before the postback is done.  I have tried returning a false from the javascript but then postback isnt being done at all.  </p>
  
  <p>I am using Masterpages in asp.net.  Also I have a similar function in another project which works fine, the only difference is in this one I'm using a MasterPage.</p>
  
  <pre><code>&lt;asp:TemplateField&gt;
  &lt;ItemTemplate&gt;
      &lt;asp:Button ID="btnAddRegistration" ClientIDMode="Static" CssClass="btn-sm btn-default" runat="server"
          CommandArgument="&lt;%# ((GridViewRow) Container).RowIndex %&gt;"
          onclick="btnAddRegistration_Click"
          OnClientClick='&lt;%# "getSetContactID(\"" + Container.DataItemIndex + "\")" %&gt;'
          Text="Add &amp;#010;Registration" /&gt;
   &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>Javascript (call to webmethod)</p>
  
  <pre><code> function getSetContactID(rowIndex) {
  
      var CellValue, cell, dataItemIndex;
      var table = document.getElementById('&lt;%=gvContactSearch.ClientID %&gt;');
  
      $.ajax({
          type: "POST",
          url: "WebService1.asmx/setContactIDgvContact",
          data: '{DataItemIndex: "' + rowIndex + '"}',
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: alert(rowIndex), &lt;--this gets called before postback
          failure: function (response) {alert(response.d);}
  
          })
   }
  </code></pre>
  
  <p>Webservice method</p>
  
  <pre><code> [WebMethod]
          public string setContactIDGV1(int DataItemIndex){
             classDetails.gV1DataItemIndex = DataItemIndex; 
             return "";
          }
  </code></pre>