Calculate total in FooterTemplate of Gridview using C# in ASP.Net

36083185a
 
on Jun 21, 2016 02:27 AM
4323 Views

hi, i'm trying to do something in a begining easy but not so in real. I have a grid view with a new row to insert new records through textboxes, among them txtUnits, txtPrice and txtTotal.

i have problems to access textbox content in the gridview. How ca i refeer to them to get Total value in txtTotal.

 

Something easy like txtTotal= txtUnits * txtPrice

this is part of the code of my Grid:

  

 

 

<asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Unidades">
    <ItemTemplate>
        <asp:Label ID="lblUnidades" runat="server"
            Text='<%# Eval("Cantidad")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtUnidades" runat="server"
            Text='<%# Eval("txtUnidades")%>'></asp:TextBox>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtUnidades" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Precio">
    <ItemTemplate>
        <asp:Label ID="lblPrecio" runat="server"
            Text='<%# Eval("precio")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtPrecio" runat="server"
            Text='<%# Eval("txtPrecio")%>'></asp:TextBox>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtPrecio" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Importe">
    <ItemTemplate>
        <asp:Label ID="lblTotal" runat="server"></asp:Label>
    </ItemTemplate>
   
</asp:TemplateField>

 

Should it be possible to calculate Total onBlur event or similar?

 

Many thanks in advance for suggestions

Download FREE API for Word, Excel and PDF in ASP.Net: Download
AnandM
 
on Jun 22, 2016 03:55 AM

Hi ,

I have created sample. Please refer the below code.

 HTML

<div>
    <asp:GridView ID="gvTotal" AutoGenerateColumns="false" runat="server" ShowFooter="true">
        <Columns>
            <asp:TemplateField HeaderText="Unit">
                <ItemTemplate>
                    <asp:TextBox ID="txtUnits" Text='<%#Eval("units") %>' runat="server"></asp:TextBox>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtFooterUnits" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Price">
                <ItemTemplate>
                    <asp:TextBox ID="txtAmount" Text='<%#Eval("amount") %>' runat="server"></asp:TextBox>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtFooterAmount" runat="server" AutoPostBack="true" OnTextChanged="txtFooterAmount_TextChanged"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total">
                <ItemTemplate>
                    <asp:Label runat="server"></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblTotal" runat="server" />
                    <asp:Button Text="Add" runat="server" OnClick="Save" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

Code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("units", typeof(double)), new DataColumn("amount", typeof(double)) });
            dt.Rows.Add();
            this.gvTotal.DataSource = dt;
            this.gvTotal.DataBind();
        }
    }

    protected void txtFooterAmount_TextChanged(object sender, EventArgs e)
    {
        TextBox textbox = sender as TextBox;
        double units = Convert.ToDouble((textbox.NamingContainer.FindControl("txtFooterUnits") as TextBox).Text);
        double amount = Convert.ToDouble((textbox.NamingContainer.FindControl("txtFooterAmount") as TextBox).Text);
        double total = units * amount;
        (textbox.NamingContainer.FindControl("lblTotal") as Label).Text = total.ToString();
    }

    protected void Save(object sender,EventArgs e)
    {
        //Your code for saving.
    }

Screenshot