0

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.

4
  • If I am changing to if(isAdmin) then it going in the condition and not other. response is vice-verse Commented Oct 3, 2016 at 10:02
  • check the typeof isAdmin. If it is string then it will always enter into the if condition. Commented Oct 3, 2016 at 10:05
  • ok its type is coming as string Commented Oct 3, 2016 at 10:06
  • I have posted answer to convert it boolean and then check. Commented Oct 3, 2016 at 10:10

4 Answers 4

2

You are setting the value of that isAdmin parameter as a string. So the check inside the "if" checks for the "truthy" value of the string, which is any non null/non empty value. So both the values 'True' and 'False' will be seen as true.

To solve this:

  1. remove the quotes around <%= Convert.ToBoolean(Utility.IsAdmin()) %>
  2. make sure you get true and false (lowercase!). You might need to add a .ToString().ToLowerInvariant(). Or (guessing that Utility.IsAdmin() returns a bool): <%= Utility.IsAdmin() ? "true" : "false" %>
Sign up to request clarification or add additional context in comments.

2 Comments

using ur point of removing quote , does not call the DisplayPrdWeightGrd at all
@HinaKhuman Did it write out True (capital T) or true (lowercase)? (it should be lowercase) What is actually in the generated html/javascript? Any errors in the javascript console?
0

Try

var isAdmin = Boolean(isAdmin);

then check if-else condition.

if(!isAdmin)

6 Comments

Still the same situation it is not going inside If(!isAdmin) condition block
@HinaKhuman what's the output of console.log(typeof isAdmin, isAdmin) after the conversion.
var Admin = typeof(isAdmin); gives me string type
var isAdmin = Boolean(isAdmin); console.log(typeof isAdmin, isAdmin); now what is consoled? copy paste the same line as in comment.
in both the case like Saler or Admin it giving true value. var Admin = Boolean(isAdmin); alert(Admin);
|
0

The isAdmin only exists inside your ajax call, thus your setDataOnRow function will not recognize it hence always go to the else

1 Comment

No it does go inside setDataOnRow. I set alert inside that function.alert gives the exact value
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.