Hina Khuman Hina Khuman - 1 month ago 5
ASP.NET (C#) Question

Going in same javascript if condition irrespective of value of given variable

Even if the value of


isAdmin = false


The code is not going into the condition

if(!isAdmin)


As a result, I am not getting desired output.
This is how below if called in asp.net :

<span onclick="DisplayPrdWeightGrd('<%#Container.ItemIndex + 1 %>','<%# Eval("OrderId")%>','<%= Convert.ToBoolean(Utility.IsAdmin()) %>');">




function DisplayPrdWeightGrd(index, OrderId, isAdmin) {
$.ajax({
type: "POST",
url: "../handlers/DisplayOrderDetail.ashx?OrderId=" + Orderid + "&tk=" + $.now(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
if ($('#tableOrderDetail_' + index + ' tr').length <= 1) {
$.each(data, function(i, v) {
setDataOnRow(index, v, i);
});
}
},
failure: function(response) {
alert("fail");
},
error: function(response) {
alert(response);
}
});

function setDataOnRow(idx, v, i) {

var totalprice;
if (v.IsGST == true) {
totalprice = parseFloat(v.TotalPrice * (1.1)).toFixed(2);
} else {
totalprice = parseFloat(v.TotalPrice).toFixed(2);
}

//debugger;

var obj = $('#tableOrderDetail_' + idx + ' tr td table:last');
if (!isAdmin) {
$('#tableOrderDetail_' + idx + ' tr:last').after('<tr>' +
'<td width="25%" >' + (i + 1) + '</td>' +
'<td width="25%" class="center">' + v.ProductName + '</td>' +
'<td width="12%" class="center">' + v.NoOfCarton + '</td>' +
'<td width="12%" class="center">' + v.ProductQuantity + '</td>' +
'<td width="12%" class="center">' + v.OriginalPrice + '</td>' +
'<td width="12%" class="center">' + v.OrderPrice + '</td>' +
'<td width="10%" class="center">' + v.IsGST + '</td>' +
'<td width="10%" class="center">' + v.Discount + '</td>' +
'<td width="12%" class="center">' + totalprice + '</td></tr>');
} else {
$('#tableOrderDetail_' + idx + ' tr:last').after('<tr>' +
'<td width="25%" >' + (i + 1) + '</td>' +
'<td width="25%" class="right">' + v.ProductName + '</td>' +
'<td width="12%" class="right">' + v.NoOfCarton + '</td>' +
'<td width="12%" class="right">' + v.ProductQuantity + '</td>' +
'<td width="12%" class="right">' + v.OriginalPrice + '</td>' +
'<td width="12%" class="right">' + v.OrderPrice + '</td>' +
'<td width="10%" class="right">' + v.IsGST + '</td>' +
'<td width="10%" class="center">' + v.Discount + '</td>' +
'<td width="12%" class="center">' + totalprice + '</td></tr>');
}
}





I tried debugger, it is always going is else condition for the value isAdmin is true or false. Stuck here.

Answer

function DisplayPrdWeightGrd(index, OrderId, isAdmin) {       var Admin = isAdmin.toLowerCase();

  $.ajax({
type: "POST",
url: "../handlers/DisplayOrderDetail.ashx?OrderId=" + Orderid + "&tk=" + $.now(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
  if ($('#tableOrderDetail_' + index + ' tr').length <= 1) {
    $.each(data, function(i, v) {
      setDataOnRow(index, v, i);
    });
  }
},
failure: function(response) {
  alert("fail");
},
error: function(response) {
  alert(response);
}
  });

  function setDataOnRow(idx, v, i) {

var totalprice;
if (v.IsGST == true) {
  totalprice = parseFloat(v.TotalPrice * (1.1)).toFixed(2);
} else {
  totalprice = parseFloat(v.TotalPrice).toFixed(2);
}

//debugger;

var obj = $('#tableOrderDetail_' + idx + ' tr td table:last');
if (Admin === "false") {
  $('#tableOrderDetail_' + idx + ' tr:last').after('<tr>' +
    '<td  width="25%" >' + (i + 1) + '</td>' +
    '<td  width="25%" class="center">' + v.ProductName + '</td>' +
    '<td width="12%" class="center">' + v.NoOfCarton + '</td>' +
    '<td width="12%" class="center">' + v.ProductQuantity + '</td>' +
    '<td width="12%" class="center">' + v.OriginalPrice + '</td>' +
    '<td width="12%" class="center">' + v.OrderPrice + '</td>' +
    '<td width="10%" class="center">' + v.IsGST + '</td>' +
    '<td width="10%" class="center">' + v.Discount + '</td>' +
    '<td width="12%" class="center">' + totalprice + '</td></tr>');
} else {
  $('#tableOrderDetail_' + idx + ' tr:last').after('<tr>' +
    '<td  width="25%" >' + (i + 1) + '</td>' +
    '<td  width="25%" class="right">' + v.ProductName + '</td>' +
    '<td width="12%" class="right">' + v.NoOfCarton + '</td>' +
    '<td width="12%" class="right">' + v.ProductQuantity + '</td>' +
    '<td width="12%" class="right">' + v.OriginalPrice + '</td>' +
    '<td width="12%" class="right">' + v.OrderPrice + '</td>' +
    '<td width="10%" class="right">' + v.IsGST + '</td>' +
    '<td width="10%" class="center">' + v.Discount + '</td>' +
    '<td width="12%" class="center">' + totalprice + '</td></tr>');
}
  }

It solved my issue using below two lines: 1. In function

function DisplayPrdWeightGrd(index, OrderId, isAdmin)

add

 var Admin = isAdmin.toLowerCase();

2 In function

function setDataOnRow(idx, v, i)

add in if condition as

    if(Admin==="false")

Thanks for discussion.

Comments