Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I want run IsValidHesNo method for all of rows of grid. but it is run for last 'i' in loop.
plz help me
hanks alot

JavaScript
var rows = document.getElementById("<%= grdSnd.ClientID %>").rows;
for (var i = 1; i < rows.length; i++) {
hesCode = rows[i].cells[0].getElementsByTagName("input")[0].value + rows[i].cells[1].getElementsByTagName("input")[0].value + rows[i].cells[2].getElementsByTagName("input")[0].value
 IsValidHesNo(hesCode, function (res) {
alert(res);
});
}
function IsValidHesNo(hesCode, callBack) {
       $.ajax({
           type: "post",
           contentType: "application/json; charset=utf-8",
           data: "{'hesCode':'" + hesCode + "'}",
           url: "insertSanad.aspx/IsValidHesNo",
           dataType: "json",
           success: function (response) {
               callBack(response.d);
           },
           Error: function () {
               callBack("");
           }
       });
   }
Posted
Comments
SrikantSahu 30-Nov-14 1:56am    
Hi,
Can you please check your boundary condition.
for( i =0; i< rows.length; i++) // here i starts from 0

1 solution

This caused because the ajax call is asynchronous.
And your varable hesCode is a global variable. So hescode will be a reference to the window.hesCode. You should use a closure.

If you put the first part containing the loop in an anonymous function and declare hesCode with a var in front of it it should work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900