简体   繁体   English

在linkbutton asp中发回之前,ajax请求不会首先运行

[英]ajax request does not run first before postback in linkbutton asp

I'm trying to run an insert statement using a button in my application. 我正在尝试在应用程序中使用按钮运行插入语句。 The thing is it is working except that I noticed that whenever I try to click the button, I noticed it would run the Page_Load first before it could run the ajax statement. 事情是正常的,除了我注意到每当我尝试单击该按钮时,我注意到它会先运行Page_Load,然后才能运行ajax语句。

what should I do to allow the ajax statement work first before the Page_Load? 我应该怎么做才能使ajax语句在Page_Load之前先工作?

This is my button: 这是我的按钮:

 <asp:LinkButton ID="uoCheckBoxTagtoVehicle" Text="Tag" Width="50px" Visible='<%# Convert.ToInt32(Eval("TaggedActive")) == 0 ? true : false %>'  OnClick='<%# "TagChanged(this, "+ Eval("colIdBigint") +", "+ Eval("colTravelReqIDInt") +", \""+ Eval("RecordLocator") +"\", "+ Eval("SeafarerIdInt") +", "+ Eval("colVehicleVendorIDInt") +", \""+ Eval("colSFStatus")+ "\" , "+ Eval("colVehicleVendorIDInt")+")" %>' runat="server" />

and this is my ajax request inside my function(which is working): 这是我在函数内的ajax请求(正在运行):

         $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "<%=ResolveClientUrl("~/PageMethods.aspx/TagtoVehicle")%>",
        data: "{'colIDBigint': '" + IDBigInt + "', 'colTravelReqIDInt': '" + TravelReq +
        "', 'colRecordLocatorVarchar': '" + RecordLoc + "', 'colSeafarerIdInt': '" + SeafarerId + "', 'colOnOff': '" + colSFStatus + "', 'colPortAgentVendorIDInt': '" + Vehiclevendor + "', 'colSFStatus': '" + onoff + "', 'UserId': '" + userId +  "'}",
        dataType: "json",
        success: function(data) {
        } ,
        error: function(objXMLHttpRequest, textStatus, errorThrown) {
        alert(errorThrown);
        }
        });  

You have declare OnClick event in your LinkButton, OnClick event execute on server side so it is obvious it will call page load method. 您已经在LinkBut​​ton中声明了OnClick事件,OnClick事件在服务器端执行,因此很明显它将调用页面加载方法。

You should use OnClientClick event instead of OnClick . 您应该使用OnClientClick事件而不是OnClick it allow to execute javascript function. 它允许执行JavaScript功能。

<asp:LinkButton ID="uoCheckBoxTagtoVehicle" Text="Tag" Width="50px" Visible='<%# Convert.ToInt32(Eval("TaggedActive")) == 0 ? true : false %>' OnClientClick="callMyFuction" runat="server" />

On javascript side 在JavaScript方面

function callMyFuction(){
//you can call your ajax request here.
}

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

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