marchemike
marchemike

Reputation: 3277

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.

what should I do to allow the ajax statement work first before the 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({
        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);
        }
        });  

Upvotes: 0

Views: 402

Answers (1)

Manish Parakhiya
Manish Parakhiya

Reputation: 3788

You have declare OnClick event in your LinkButton, OnClick event execute on server side so it is obvious it will call page load method.

You should use OnClientClick event instead of OnClick. it allow to execute javascript function.

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

On javascript side

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

Upvotes: 1

Related Questions