Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
product Name---in Label
Price ------in Label
quantity-----in TextBox
total cost and Grand total -----in label control

First Task
product name  price  quantity  total cost
a               30     2          60 

product name  price  quantity  total cost
b              20     1          20

Grand Total : 80

here when we enter quantity 2 then ---immediatly total cost and grand total will calculate

Second Task
product name   price  quantity  total cost
a               30     --          0

product name  price  quantity  total cost
b              20     0         0

Grand Total : 0

here when quantity is nothing (empty) or contain 0 then total cost and grand total will calculate 0

above first task i do properly but second task have problem please help

my code
JavaScript
<script type="text/javascript">
    $(function () {
        $("[id*=txtQuantity]").val("0");
    });
    $("[id*=txtQuantity]").live("change", function () {
        if (isNaN(parseInt($(this).val()))) {
            $(this).val('0');
        } else {
            $(this).val(parseInt($(this).val()).toString());
        }
    });
    $("[id*=txtQuantity]").live("keyup", function () {
        if (!jQuery.trim($(this).val()) == '') {
            if (!isNaN(parseFloat($(this).val()))) {
                var row = $(this).closest("tr");
                $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
            }
        } else {
            $(this).val();

            
        }
        var grandTotal = 0;
        $("[id*=lblTotal]").each(function () {
            grandTotal = grandTotal + parseFloat($(this).html());
        });
        $("[id*=lblGrandTotal]").html(grandTotal.toString());
    }
);
</script>

HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Product_Name" HeaderText="Item" />
        <asp:BoundField DataField="Unit_Price" HeaderText="Price" ItemStyle-CssClass="price" />
        <asp:TemplateField HeaderText = "Quantity">
            <ItemTemplate>
                <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText = "Total">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Grand Total:
<asp:Label ID="lblGrandTotal" runat="server" Text="0"></asp:Label>

C#
private void FillGrid()
  {
    SqlConnection con=new SqlConnection(StrConnection);
       con.Open();
      DataTable dt = new DataTable();
       SqlCommand cmd=new SqlCommand("SP_GetProductDetail",con);
       cmd.CommandType=CommandType.StoredProcedure;

       SqlDataAdapter da=new SqlDataAdapter(cmd);
       da.Fill(dt);

      GridView1.DataSource = dt;
      GridView1.DataBind();
  }
Posted
Updated 28-Nov-14 21:17pm
v3

1 solution

Hi Rajnish,

I have done few modification to the code. Now checking for NaN or parsing not required as the textbox wont allow non-numeric characters.
Also added blur event to the quantity textbox such that tab out keys will be supported.
JavaScript
$(document).ready(function () {
            $(function () {
                $("[id*=txtQuantity],[id*=lblTotal],[id*=lblGrandTotal]").val("0");
            });

            $("[id *= txtQuantity]").keypress(function (e) {
                if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                    return false;
                }
            });

            $("[id*=txtQuantity]").on("keyup blur", function () {
                var row = $(this).closest("tr");
                $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat(jQuery.trim($(this).val()) == '' ? 0 : $(this).val()));
                var grandTotal = 0;
                $("[id*=lblTotal]").each(function () {
                    grandTotal = grandTotal + parseFloat($(this).html());
                });
                $("[id*=lblGrandTotal]").html(grandTotal.toString());
            });
        });


Thanks
Srikant
 
Share this answer
 
Comments
Rajnish D mishra 30-Nov-14 4:31am    
thanx @srikant

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