简体   繁体   English

在onclick之后执行onclick和onclientclick webmethod

[英]onclick and onclientclick webmethod executed after onclick

I am trying to set a variable on using a webmethod ( .asmx ) 我试图使用webmethod( .asmx )设置变量

The button is using onclick to call a server side method and onclientclick to call a javascript ajax which calls the webmethod. 该按钮使用onclick调用服务器端方法, onclientclick调用调用onclientclick的javascript ajax。

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. 我需要在调用onclick服务器端方法之前完全执行javascript代码,但似乎在回发之前没有完全执行ajax调用。

The alert is called before postback however the webmethod is being called after the postback. 在回发之前调用警报,但是在回发之后调用web方法。 I need the variable to be set from the webmethod before the postback is done. 我需要在回发完成之前从webmethod设置变量。 I have tried returning a false from the javascript but then postback isnt being done at all. 我试过从javascript返回一个假,但后来回发根本没有完成。

I am using Masterpages in asp.net. 我在asp.net中使用Masterpages。 Also I have a similar function in another project which works fine, the only difference is in this one I'm using a MasterPage. 此外,我在另一个项目中有一个类似的功能,工作正常,唯一的区别是在这一个我使用MasterPage。

<asp:TemplateField>
<ItemTemplate>
    <asp:Button ID="btnAddRegistration" ClientIDMode="Static" CssClass="btn-sm btn-default" runat="server"
        CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
        onclick="btnAddRegistration_Click"
        OnClientClick='<%# "getSetContactID(\"" + Container.DataItemIndex + "\")" %>'
        Text="Add &#010;Registration" />
 </ItemTemplate>
</asp:TemplateField>

Javascript (call to webmethod) Javascript(调用webmethod)

 function getSetContactID(rowIndex) {

    var CellValue, cell, dataItemIndex;
    var table = document.getElementById('<%=gvContactSearch.ClientID %>');

    $.ajax({
        type: "POST",
        url: "WebService1.asmx/setContactIDgvContact",
        data: '{DataItemIndex: "' + rowIndex + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: alert(rowIndex), <--this gets called before postback
        failure: function (response) {alert(response.d);}

        })
 }

Webservice method Web服务方法

 [WebMethod]
        public string setContactIDGV1(int DataItemIndex){
           classDetails.gV1DataItemIndex = DataItemIndex; 
           return "";
        }

What you have here is a classic race-condition scenario. 你在这里有一个经典的竞赛条件。

By default an AJAX call is asynchronous, so when you fire an AJAX call the JS code block logically comes to an end, and the next sequential event is fired, in this case your server side event. 默认情况下,AJAX调用是异步的,因此当您触发AJAX调用时,JS代码块在逻辑上结束,并触发下一个顺序事件,在这种情况下是您的服务器端事件。 It's then a race between the function that the AJAX code has executed and your server-side onclick event. 然后是AJAX代码执行的函数和服务器端的onclick事件之间的竞争。 The way I see it you have a few options: 我看到它的方式你有几个选择:

  1. Call your web service method inside your server-side on click method 通过单击方法在服务器端调用Web服务方法
  2. Prevent the default element action inside the JS block ( event.preventDefault ), and hook something up inside the AJAX success method that finishes the sequence 防止JS块( event.preventDefault )中的默认元素操作,并在完成序列的AJAX success方法中挂钩
  3. Set the async: false option on the AJAX call 在AJAX调用上设置async: false选项

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

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