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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pradeep ShetRank #595 · 4K reputationPosted Sep 29, 2014, 5:11 AM EDT
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
muthukumar146 reputationPosted Sep 29, 2014, 6:11 AM EDT
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());
});
});