1

我正在尝试合计添加到 gridview 的所有产品的价格和数量,但我似乎无法弄清楚为什么总数没有显示在页脚中。我为vb拥有的代码应该将数量乘以价格并将其放入gridview的页脚中。gridview 页脚是可见的,所以我知道这不是问题所在。任何帮助,将不胜感激。

ASP:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataSourceID="Cart" AllowSorting="True" BackColor="White" 
    BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
    GridLines="Vertical" ShowFooter="True" AutoGenerateEditButton="True" 
    AutoGenerateDeleteButton="True" DataKeyNames="cartID">
    <AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:BoundField DataField="cartID" HeaderText="cartID" SortExpression="cartID" 
        InsertVisible="False" ReadOnly="True" Visible="False"></asp:BoundField>
    <asp:BoundField DataField="cartNO" HeaderText="cartNO" SortExpression="cartNO" 
        Visible="False" />
    <asp:BoundField DataField="productID" HeaderText="productID" 
        SortExpression="productID" InsertVisible="False" ReadOnly="True" />
    <asp:BoundField DataField="productName" HeaderText="productName" 
        SortExpression="productName" InsertVisible="False" ReadOnly="True" />
    <asp:BoundField DataField="price" HeaderText="price" 
        SortExpression="price" InsertVisible="False" ReadOnly="True" />
    <asp:BoundField DataField="quantity" HeaderText="quantity" 
        SortExpression="quantity" />
        <asp:TemplateField HeaderText="SubTotal" SortExpression="subTotal" >
        <ItemTemplate>
        <%# Eval("price") * Eval("quantity")%>
        </ItemTemplate>
       <%-- <FooterTemplate>
      <asp:Label ID="sum" runat="server"/>
        </FooterTemplate>--%>
        </asp:TemplateField>

VB公共类MyCart

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim strcartNO As String = ""
    Dim cookieBack As HttpCookie
    cookieBack = HttpContext.Current.Request.Cookies("cartNO")
    strcartNO = cookieBack.Value
    'sqldscartLine.selectCommand = "Select * from cartLine where cartNO = '" & strcartNO & "'"
    GridView1.DataBind()


End Sub

Public Shared Sub DeleteMethod(ByVal original_OrderID As Integer, _
ByVal original_ProductID As Integer)

End Sub



Dim priceTotal As Decimal = 0
Dim quantityTotal As Integer = 0
Sub GridView1_RowDataBound(ByVal sender As Object, _
  ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        ' add the UnitPrice and QuantityTotal to the running total variables
        priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
         "price"))
        quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
          "quantity"))
    ElseIf e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(2).Text = "Totals:"
        ' for the Footer, display the running totals
        e.Row.Cells(3).Text = priceTotal.ToString("c")
        e.Row.Cells(4).Text = quantityTotal.ToString("d")

        e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
        e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right
        e.Row.Font.Bold = True
    End If
End Sub
4

2 回答 2

0

不确定是否有人看到评论,所以将其放在答案中 - 这可能会帮助其他人(如果我是正确的..)

您的代码包含以下内容

 <%-- <FooterTemplate>
  <asp:Label ID="sum" runat="server"/>
    </FooterTemplate>--%>

<%--and是注释,所以你的--%>页脚被注释掉了。

将其更改为

 <FooterTemplate>
  <asp:Label ID="sum" runat="server"/>
    </FooterTemplate>

它应该显示。

于 2012-10-31T17:25:25.910 回答
0

数据绑定创建数据行。页脚不是数据行,因此在RowDataBound创建事件时不会调用该事件。因此,您的事件处理程序GridView1_RowDataBound将永远不会执行生成总数的代码,因为表达式

e.Row.RowType = DataControlRowType.Footer 

...在该方法的任何执行过程中永远不会是真的。

尝试处理RowCreated事件,如下所示:

Sub GridView1_RowCreated(ByVal sender As Object, _
  ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(2).Text = "Totals:"
        ' for the Footer, display the running totals
        e.Row.Cells(3).Text = priceTotal.ToString("c")
        e.Row.Cells(4).Text = quantityTotal.ToString("d")

        e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
        e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right
        e.Row.Font.Bold = True
    End If
End Sub
于 2012-10-31T19:22:38.460 回答