Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to add value of all textboxes inside gridview using Jquery. Gridview is itself inside
a datalist.

Datalist and its gridviews are binded dynamically.

I want to display the sum of textboxes for each gridview.

Please provide me help.

Thanks in advance.

What I have tried:

//// Jquery Code
$(document).ready(function () {
$(".txt_style").keyup(function () {
var grandTotal = 0;
$("[id*=txtValue]").each(function (index) {
var value = $(this).val();
if (value != "" && value != ".") {
grandTotal = grandTotal + parseFloat(value);
}
});
$("[id*=lblTotal]").html(grandTotal.toString());

});
});

/// HTML

<asp:DataList ID="DetBillType" runat="server" OnItemDataBound="DetBillType_ItemDataBound" RepeatColumns="2" RepeatDirection="Horizontal">

<itemtemplate>

<asp:GridView ID="grdBillingDetails" runat="server" AutoGenerateColumns="false"
CellPadding="2" CellSpacing="2"
HorizontalAlign="Center" ShowFooter="true" AlternatingRowStyle-CssClass="alt"
CssClass="Grid"
Width="100%" OnRowDataBound="grdBillingDetails_RowDataBound">


<columns> <asp:TemplateField HeaderText="Bill Type">
<itemtemplate>
<asp:Label ID="lblBillType" runat="server" Text='<%#Eval("Description")+" *"%>'>

<footertemplate>

<asp:Label ID="lblttotal" runat="server" Text="Total Amount" />




<itemstyle cssclass="gridItemStyle">
<HeaderStyle HorizontalAlign="Left" />

<asp:TemplateField HeaderText="">
<itemtemplate>

<asp:TextBox ID="txtValue" runat="server" MaxLength="10" Text='<%#Eval("VALUE")%>'
CssClass="txt_style" onKeyPress="return numbersonly(event, true)" autocomplete="off">



<footertemplate>





<asp:Label ID="lblTotal" runat="server" Text="">









Posted
Updated 1-Aug-16 3:37am
Comments
Suvendu Shekhar Giri 1-Aug-16 5:36am    
So, what is the issue?
Member 12663665 1-Aug-16 6:09am    
Dear Sir actually this code is calculating total of textboxes of all gridviews but i need separate total count for textboxes of each grid.

1 solution

You're currently finding every textbox whose ID contains the string txtValue:
JavaScript
$("[id*=txtValue]")

If you only want to find the textboxes within the current grid, then you need to limit the scope of your search:
JavaScript
$(this).closest("table").find("[id*=txtValue]")

Each GridView renders an HTML <table>. You need to find the nearest <table> which is an ancestor of the current element, and then find all matching textboxes within that <table>.
 
Share this answer
 
Comments
Member 12663665 2-Aug-16 6:52am    
Thank you so much Richard for your solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900