javascript to caluculate two textbox values and display in another text box inside gridview i
also use these in viewstate for further steps
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
muthukumarPosted Sep 29, 2014, 10:11 AM
my code is in javascript
to calculate two textbox value and display it in label
below i attach my code
$(function () {
$("[id*=grvStudentDetails]input[type=text][id*=txt_price]").keyup(function (e)
{
var price = $(this).closest('tr').find("input[type=text][id*=txt_price]").val();
var quantity = $(e.target).closest('tr').find("input[type=text][id*=txt_quantity]").val();
var total = parseInt(price) * parseInt(quantity);
////$(e.target).closest('tr').find("[id*=txt_totalprice]").text(total);
$(e.target).closest('tr').find("[id*=lb_totalprice]").text(total);
var grandTotal = 0;
$("[id*=lb_totalprice]").each(function ()
{
grandTotal = grandTotal + parseFloat($(this).html());
});
$("[id*=lblGrandTotal]").html(grandTotal.toString());
});
});
Pradeep ShetPosted Sep 29, 2014, 9:11 AM
First of all value cant be store in viewstate instead store in hiddenfield. Considering the scenario you have 2 input text box, 1 textbox for result & button to calculate for each row.
Call this Calculate function on button click passing this as parameter. We will get the Id of that button of clicked row. Now I have searched for other textboxes using the id and replacing the last part of id with our textbox id as previous part remains same for every element of that row in gridview.
function Calculate(btnObj)
{
var btnId = $(btnObj).attr('id');
var txt1Id = btnId.replace('_btn','_txt1');
var txt2Id = btnId.replace('_btn','_txt2');
var resultId = btnId.replace('_btn','_txtresult');
var hid = btnId.replace('_btn','_hdnResult');
var total = parseFloat($('#'+txt1Id).val()) + parseFloat($('#' + txt2Id).val());
$('#' +resultId).val(total);
$('#' + hid).val(total);
}
Hope this answers your query. If yes plz mark it as answered