Calculate total price on Textbox change event

By Rampoojan Gupta Dec 24, 2011
Here I am going to explain how to calculate total price on textbox change event in GridView.

How to calculate Total price on textbox change event in Gridview.

Step 1 : Apply JavaScript code under a <head > tag.

    <script type="text/javascript">
        function CalcSellPrice2(CurrentPrice, QuntValue, TotalValue) {
            var QuntVar = parseFloat(document.getElementById(QuntValue).value);
            var TotalVar = document.getElementById(TotalValue);
            var TotalPriceValue = parseFloat(CurrentPrice * QuntVar);
            TotalVar.innerHTML = TotalPriceValue;

Step 2 : Grid View Source Code.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
                <asp:TemplateField HeaderText="List Price" Visible="True">
                        <asp:Label ID="lblCurrentListPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
                <asp:TemplateField HeaderText="Qunt">
                        <asp:TextBox ID="txtqut" Width="100px" runat="Server" />
                <asp:TemplateField HeaderText="Total Price">
                        <asp:Label ID="lblTotal" runat="Server" />

Step 3 : Apply code on page.cs part.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;|
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
protected void Page_Load(object sender, EventArgs e)
        GridView1.DataSource = GetData();
    public DataSet GetData()
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("Product");
        DataRow dr;
        dt.Columns.Add(new DataColumn("Price", typeof(Int32)));
        dt.Columns.Add(new DataColumn("Qut", typeof(Int32)));
        dt.Columns.Add(new DataColumn("TotalPrice", typeof(Int32)));
        for (int i = 1; i <= 10; i++)
            dr = dt.NewRow();
            dr[0] = (20 * i);
        Session["dt"] = dt;
        return ds;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            Label lblCurrentListPrice = (Label)e.Row.FindControl("lblCurrentListPrice");
            Double CurPr = Convert.ToDouble(lblCurrentListPrice.Text);
            TextBox txtqut = (TextBox)e.Row.FindControl("txtqut");
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            txtqut.Attributes.Add("onkeyup", "CalcSellPrice2(" + CurPr + ", '" + txtqut.ClientID + "','" + lblTotal.ClientID + "')");           


Step 4 : Run page and getting out put thanks.

Rampoojan Gupta
Rampoojan Gupta

Having 4+ years of experience in Software Development, with the ASP.Net (DHTML, C#, CSS), AJAX, JavaScript, MS SQL 2005/2008, SharePoint, Silverlight, WCF and Dotnetnuke for Content Management System skills.