<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="" Inherits="cart" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script runat="server">
Dim TotalUnitPrice As Decimal = 0.0
Function GetUnitPrice(ByVal Price As Decimal) As Decimal
TotalUnitPrice += Price
Return Price
End Function
Function GetTotal() As Decimal
Return TotalUnitPrice
End Function
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Panel ID="Panel1" runat="server" Height="196px"
style="margin-bottom: 49px">
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
ForeColor="Black" GridLines="Vertical"

style="z-index: 1; left: 293px; top: 711px; position: absolute; height: 184px; width: 985px"
AutoGenerateColumns="False" DataKeyNames="OrderID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False"
ReadOnly="True" SortExpression="OrderID" />
<asp:BoundField DataField="CusID" HeaderText="CusID" SortExpression="CusID" />
<asp:BoundField DataField="CusUsername" HeaderText="CusUsername"
SortExpression="CusUsername" />
<asp:BoundField DataField="ProductDesc" FooterText="Total" HeaderText="ProductDesc"
SortExpression="ProductDesc" />
<asp:BoundField DataField="TotalQuantity" HeaderText="Quantity"
SortExpression="TotalQuantity" />

<asp:TemplateField HeaderText="Unit Price" FooterStyle-Font-Bold="True">
<ItemTemplate>
<%#GetUnitPrice(Decimal.Parse(Eval("TotalPrice").ToString())).ToString("N2")%>
</ItemTemplate>
<FooterTemplate>
<%# GetTotal().ToString("N2") %>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</asp:Panel>
<p>
<asp:Label ID="lbltotal" runat="server" Text="Total:"></asp:Label>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:BookstoreConnectionString %>"
DeleteCommand="DELETE FROM OrderDetails WHERE (OrderID = @OrderID)"
SelectCommand="SELECT * FROM [OrderDetails]">
<DeleteParameters>
<asp:Parameter Name="OrderID" />
</DeleteParameters>
</asp:SqlDataSource>
&nbsp;
</p>
<p>
</p>
</asp:Content>

Dani AI

Generated

The approach shown by (accumulating a page-level Decimal from inline binding expressions and rendering it in the FooterTemplate) is common but fragile: it depends on the exact binding order and on not rebinding the GridView unexpectedly. A more reliable pattern is to keep a running total during the GridView.RowDataBound event and then write the final value into the footer row when the footer is bound. The RowDataBound technique is the pattern used in Microsoft’s GridView examples for displaying totals. (learn.microsoft.com)

A compact, practical implementation (VB.NET) looks like this:

' markup: add OnRowDataBound and ShowFooter="True"
<asp:GridView ID="GridView1" runat="server" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound" ...>

' code-behind
Private _runningTotal As Decimal = 0D

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim val As Decimal = 0D
        Decimal.TryParse(DataBinder.Eval(e.Row.DataItem, "TotalPrice").ToString(), val)
        _runningTotal += val
    ElseIf e.Row.RowType = DataControlRowType.Footer Then
        ' either set a footer cell by index:
        e.Row.Cells(4).Text = _runningTotal.ToString("N2")
        ' or find a control in FooterTemplate and set its Text:
        ' Dim lbl As Label = CType(e.Row.FindControl("lblFooterTotal"), Label)
        ' If lbl IsNot Nothing Then lbl.Text = _runningTotal.ToString("N2")
    End If
End Sub

Notes and quick troubleshooting:

  • Confirm ShowFooter="True". (learn.microsoft.com)
  • If columns include CommandField/BoundField/TemplateField, cell indexes shift — prefer FindControl when footers are inside TemplateField.
  • Do not call GridView.DataBind() on every Page_Load; only bind when Not IsPostBack, otherwise row-binding logic runs twice or order changes.
  • If the intent is a grand total across all pages, compute it in SQL (e.g., SELECT SUM(TotalPrice) FROM OrderDetails) and display it outside the per-page footer instead of summing only visible rows.

This RowDataBound pattern is robust for showing totals and avoids inline-binding ordering pitfalls. (learn.microsoft.com)

I just added the show footer,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowFooter= "true" BackColor="White" .....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.