Calculate total price on Textbox change event

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;
        }
    </script>

Step 2 : Grid View Source Code.

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

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();
        GridView1.DataBind(); 
    }
    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);
            dt.Rows.Add(dr);
        }
        ds.Tables.Add(dt);
        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.

Ebook Download
View all
Learn
View all