How to Get Sum of Gridview TextBox value Column on keypress ?

Kailash Singh
Kailash...
206 Points
23 Posts
<asp:GridView ID="grdPettyHead" runat="server" AutoGenerateColumns="False" DataKeyNames="PettyID" class="table table-bordered table-hover table-striped" HeaderStyle-CssClass="gvHeaderCls"
CellPadding="4" EmptyDataText="No Record Found" GridLines="Both" ShowFooter="true" ShowHeaderWhenEmpty="True" OnRowDataBound="grdPettyHead_RowDataBound"
AllowPaging="True" AlternatingRowStyle-CssClass="active success" PageSize="25">
<Columns>

<asp:TemplateField>
<HeaderTemplate>
<header>Collected Amount</header>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAmount" runat="server" onkeypress="ShowTotalAmount();" Text='<%# Eval("Amount") %>' CssClass="form-control"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:HiddenField ID="hdnCollectedAmount" runat="server" />
<asp:Label ID="lblCollectedAmount" runat="server" style="font-size:14px; font-weight:bold;"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

 Hi,  I want total sum in lblCollectedAmount Label when on key press event fire in Amount Column using javascript or jquery.

Views: 13731
Total Answered: 3
Total Marked As Answer: 0
Posted On: 30-May-2017 03:49

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
4950 Points
30 Posts
         

Use following jquery method

function ShowTotalAmount() {
  var quantity = 0;
  $("#<%=grdPettyHead.ClientId%> tr").each(function () {
     $this = $(this);
     if ($this.find('input[type=text]').val() != undefined) {
           quantity += parseFloat($this.find('input[type=text]').val());
     }
  });
  $("#<%=lblCollectedAmount.ClientId%>").html(quantity);
}
Posted On: 30-May-2017 04:15
Kailash Singh
Kailash...
206 Points
23 Posts
         

sir,  lblCollectedAmount control inside grid view so i am unable to getting its id.

Posted On: 30-May-2017 22:47
Rahul Maurya
Rahul M...
4950 Points
30 Posts
         

You can use this

$("#<%=((Label)grdPettyHead.FooterRow.FindControl("lblCollectedAmount")).ClientID%>").html(quantity);


Or, 
Add css class in label control and do this

$('.yourLabelClass').html(quantity);
Posted On: 30-May-2017 23:58
 Log In to Chat