尝试在asp中完成gridview

时间:2012-10-31 11:07:37

标签: asp.net vb.net

我试图总计添加到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

2 个答案:

答案 0 :(得分:0)

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

您的代码在

中有以下内容
 <%-- <FooterTemplate>
  <asp:Label ID="sum" runat="server"/>
    </FooterTemplate>--%>

<%----%>是评论,因此您的页脚已被注释掉。

将其更改为

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

它应该显示。

答案 1 :(得分: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