Click here to Skip to main content
14,668,498 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

If you only want to find the textboxes within the current grid, then you need to limit the scope of your search:
$(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>.
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100