Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
  1. The content page has two Gridviews, gv_Purchase and gv_PurchaseDetails
  2. On Page_Load, gv_Purchase is displaying data from tbl_Purchase table of SQL.
  3. To get information in detail users have to select any particular row by clicking on corresponding link button ("Select").
  4. gv_PurchaseDetails will be visible and will display the related data regarding selected row of previous Gridview (gv_Purchase) from tbl_PurchaseDetails table of SQL.
  5. gv_PurchaseDetails has two template fields, Rate and Quantity. Users can change value from EditTemplate mode by clicking on link button (Edit).
  6. When users enter value in txt_Rate and txt_Quantity, onkeyup event calls a function to display value (txt_Rate * txt_Quantity) in label_Amount of the row. After that users will/have to store data by clicking on link button (Update).

I am unable to execute point no.6. Please let me know how should I send value of JavaScript function to textboxes in gridview.

What I have tried:

JavaScript
function CalculationTotal() {
    var grid = document.getElementById("<%= gv_PurchaseDetails.ClientID%>");
    for (var i = 0; i < grid.rows.length - 1; i++) {
        var rate = $("input[id*=txt_Rate]");
        var qty = $("input[id*=txt_Quantity]");
        var totalamount = rate[i].value * qty[i].value;
                    
        $("input[id*=lbl_Amount]").html(totalamount.toString());
    }
}
Posted
Updated 7-May-20 3:58am
v3

1 solution

Given the name, I'm assuming label_Amount (or is it lbl_Amount?) is an <asp:Label> control. That will render a <span>, not an <input>, so $("input[id*=lblAmount]") won't find anything.

You'll also need to scope your selectors to the table row you're processing, and skip the header and footer row(s).

Try:
JavaScript
function CalculationTotal() {
    var grid = document.getElementById("<%= gv_PurchaseDetails.ClientID %>");
    $(grid).find("tbody > tr").each(function(){
        var rate = $("input[id*=txt_Rate]", this);
        var qty = $("input[id*=txt_Quantity]", this);
        
        var totalAmount = rate.val() * qty.val();
        $("span[id*=lbl_Amount]", this).text(totalAmount.toString());
    });
}
 
Share this answer
 
Comments
Member 14823087 7-May-20 11:55am    
<asp:templatefield headertext="Amount">
<edititemtemplate>
<asp:label id="label_Amount" runat="server" text="<%# Eval("tamt")%>">

<itemtemplate>
<asp:label id="ItmTmp_lbl_Amount" runat="server" text="<%# Eval("tamt")%>">

<itemstyle horizontalalign="Center">
Richard Deeming 7-May-20 11:59am    
As I said, an <asp:Label> will render a <span> element, not an <input> element.

Have you tried the code from my answer?
Member 14823087 7-May-20 12:00pm    
Thank you. now it working as per your advice :)

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