What i have to do simply have to add the textbox value to bound field which is already bound with the database field.
since the .selectorClass always changes once the txtAnyOther enters, you could use a hidden input field to persist the initial value of bound field. please check the code below.
Rameezwaheed
Contributor
3730 Points
1595 Posts
Adding Gridview Textbox value to an other bound field using Jquery
Nov 17, 2012 06:18 AM|LINK
Hi all ,
I have to add the textbox value to bound field Column using Jquery i have used the below code it gives me wrong sum of values
<asp:TemplateField HeaderText="Any other"> <ItemTemplate> <asp:TextBox ID="txtAnyOther" runat="server" class="calculate" onkeyup="calculate()"></asp:TextBox> </ItemTemplate> </asp:TemplateField>and Below is the code for Bound Field
<asp:BoundField DataField="GrossPA" HeaderText="Gross <br/> P & A" ItemStyle-CssClass="selectorClass"> <ItemStyle Wrap="true" /> </asp:BoundField>and Here is Jquery Code
<script language="javascript" type="text/javascript"> function calculate() { var txtTotal = 0.00; $(".calculate").each(function (index, value) { var val = value.value; val = val.replace(",", "."); var GrossPay = parseFloat($(this).closest("tr").find(".selectorClass").html()); txtTotal = MathRound(parseFloat(GrossPay) + parseFloat(val)); if (isNaN(txtTotal)) txtTotal = 0; $(this).closest("tr").find(".selectorClass").html(txtTotal); }); } function MathRound(number) { return Math.round(number * 100) / 100; } </script>Please suggest where iam doing wrong . i need to add the Textbox value to Bound Field and when zero then it will show the original value.
Regards,
Mark as an answer if it helps
Yanping Wang...
Star
14871 Points
1529 Posts
Microsoft
Re: Adding Gridview Textbox value to an other bound field using Jquery
Nov 19, 2012 01:05 AM|LINK
Hi Rameezwaheed,
Thanks for your post.
For each row you want to get closest bound field in BoundField, so you don't need loop throught all class='calculate'
I modified your code a little please check out
<asp:TextBox ID="txtAnyOther" runat="server" class="calculate" onkeyup="calculate(this)"></asp:TextBox> <script language="javascript" type="text/javascript"> function calculate(obj) { var txtTotal = 0.00; //$(".calculate").each(function (index, value) { var val = obj.value; val = val.replace(",", "."); var GrossPay = parseFloat($(obj).closest("tr").find(".selectorClass").html()); txtTotal = MathRound(parseFloat(GrossPay) + parseFloat(val)); if (isNaN(txtTotal)) txtTotal = 0; $(obj).closest("tr").find(".selectorClass").html(txtTotal); // }); } function MathRound(number) { return Math.round(number * 100) / 100; } </script>Please let me know how it works in your side, thanks.
Feedback to us
Develop and promote your apps in Windows Store
Rameezwaheed
Contributor
3730 Points
1595 Posts
Re: Adding Gridview Textbox value to an other bound field using Jquery
Nov 19, 2012 05:10 AM|LINK
Thanks for reply
I Test the above code it is adding digit wise please have a look into below
222 246
When iam adding digit 2 it is adding 2 and when again i added entered 2 it is adding 4 and then 6 like wise.
What i have to do simply have to add the textbox value to bound field which is already bound with the database field.
Any Idea
Regards,
Mark as an answer if it helps
Rameezwaheed
Contributor
3730 Points
1595 Posts
Re: Adding Gridview Textbox value to an other bound field using Jquery
Nov 20, 2012 05:01 AM|LINK
Any Idea?
Regards,
Mark as an answer if it helps
Rameezwaheed
Contributor
3730 Points
1595 Posts
Re: Adding Gridview Textbox value to an other bound field using Jquery
Nov 20, 2012 05:01 AM|LINK
Any Idea?
Regards,
Mark as an answer if it helps
Yanping Wang...
Star
14871 Points
1529 Posts
Microsoft
Re: Adding Gridview Textbox value to an other bound field using Jquery
Nov 20, 2012 07:41 AM|LINK
Hi Rameezwaheed,
Thanks for your reply.
since the .selectorClass always changes once the txtAnyOther enters, you could use a hidden input field to persist the initial value of bound field. please check the code below.
add hidden textbox ID='tb'
<asp:TemplateField HeaderText="Any other"> <ItemTemplate> <asp:TextBox ID="txtAnyOther" runat="server" class="calculate" onkeyup="calculate(this)"></asp:TextBox> <asp:TextBox ID="tb" runat="server" Text='<%#Eval("GrossPA") %>' style='display:none;'></asp:TextBox> </ItemTemplate> </asp:TemplateField>javascript:
function calculate(obj) { var txtTotal = 0.00; //$(".calculate").each(function (index, value) { var newVal = obj.value; newVal = newVal.replace(",", "."); // var GrossPay = parseFloat($(obj).closest("tr").find(".selectorClass").html()); var row = $(obj).closest("tr"); var initalVal = row.find("[id*=tb]").val();alert(initalVal); txtTotal = MathRound(parseFloat(initalVal) + parseFloat(newVal)); if (isNaN(txtTotal)) txtTotal = 0; $(obj).closest("tr").find(".selectorClass").html(txtTotal); // }); }Thanks.
Feedback to us
Develop and promote your apps in Windows Store