2012-10-31 44 views
1

我試圖將所有添加到gridview的產品的價格和數量總和,我似乎無法弄清楚爲什麼總沒有顯示在頁腳。我對vb的代碼應該將數量與價格相乘,並將其放入gridview的頁腳。 gridview頁腳是可見的,所以我知道這不是問題。任何幫助,將不勝感激。試圖在asp中總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

請問你原來的源代碼已經頁腳註釋掉呢? - 你的腳註包裹在<%-- --%> – Darren

回答

0

不知道是否有人看到了評論,所以纔會把它放在一個答案,而不是 - 這可能幫助別人了(如果我'm correct ..)

您的代碼在

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

<%----%>是註釋,所以您的頁腳正在被註釋掉。

將其更改爲

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

它應該顯示。

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