试图在ASP GridView的总 [英] trying to total gridview in asp
问题描述
我试图总共全部加在GridView中产品的价格和数量,我似乎无法弄清楚为什么总不能在页脚显示。在code,我有VB的应该乘以数量与价格,放入GridView控件的页脚。 GridView的页脚是可见的,所以我知道这不是问题。任何帮助将是AP preciated。
ASP:
< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
的DataSourceID =购物车AllowSorting =真背景色=白
BORDERCOLOR =#999999边框=无边框宽度=1px的CELLPADDING =3
网格线=垂直ShowFooter =真AutoGenerateEditButton属性=真
AutoGenerateDeleteButton =真的DataKeyNames =cartID>
< AlternatingRowStyle背景色=亮灰/>
<柱体和GT;
< ASP:BoundField的数据字段=cartID的HeaderText =cartIDSORTEX pression =cartID
InsertVisible =FALSE只读=真可见=假>< / ASP:BoundField的>
< ASP:BoundField的数据字段=cartNO的HeaderText =cartNOSORTEX pression =cartNO
可见=FALSE/>
< ASP:BoundField的数据字段=产品的HeaderText =产品
SORTEX pression =产品InsertVisible =FALSE只读=真/>
< ASP:BoundField的数据字段=产品名称的HeaderText =产品名称
SORTEX pression =产品名称InsertVisible =FALSE只读=真/>
< ASP:BoundField的数据字段=价格的HeaderText =价格
SORTEX pression =价格InsertVisible =FALSE只读=真/>
< ASP:BoundField的数据字段=量的HeaderText =量
SORTEX pression =量/>
< ASP:的TemplateField的HeaderText =小计SORTEX pression =小计>
<&ItemTemplate中GT;
<%#的eval(价格)*的eval(量)%GT;
< / ItemTemplate中>
<% - < FooterTemplate>
< ASP:标签ID =总和=服务器/>
< / FooterTemplate> - %GT;
< / ASP:的TemplateField>
VB
公共类MyCart
继承System.Web.UI.Page保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
昏暗strcartNO作为字符串=
昏暗cookieBack作为的HttpCookie
cookieBack = HttpContext.Current.Request.Cookies(cartNO)
strcartNO = cookieBack.Value
'sqldscartLine.selectCommand =SELECT * FROM cartLine那里cartNO ='&放大器; strcartNO&安培; '
GridView1.DataBind()
结束小组公共共享子DeleteMethod(BYVAL original_OrderID整数,_
BYVAL original_ProductID作为整数)结束小组昏暗priceTotal为十进制= 0
昏暗quantityTotal为整数= 0
子GridView1_RowDataBound(BYVAL发件人为对象,_
BYVAL E上GridViewRowEventArgs)
如果e.Row.RowType = DataControlRowType.DataRow然后
添加单价和QuantityTotal到正在运行的总变数
priceTotal + = Convert.ToDecimal(DataBinder.Eval的(e.Row.DataItem,_
价钱))
quantityTotal + = Convert.ToInt32(DataBinder.Eval的(e.Row.DataItem,_
数量))
elseif的e.Row.RowType = DataControlRowType.Footer然后
e.Row.Cells(2)。文本=总计:
页脚,显示正在运行的总计
e.Row.Cells(3)。文本= priceTotal.ToString(C)
e.Row.Cells(4)。文本= quantityTotal.ToString(D) e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right
e.Row.Font.Bold = TRUE
万一
结束小组
数据绑定创建数据行。页脚不是数据行,所以它的创建当事件的RowDataBound
不叫。因此,事件处理程序 GridView1_RowDataBound
绝不会执行code产生的总数,因为前pression
e.Row.RowType = DataControlRowType.Footer
...这方法的任何执行期间将永远是正确的。
尝试处理 RowCreated
事件代替,像这样:
子GridView1_RowCreated(BYVAL发件人为对象,_
BYVAL E上GridViewRowEventArgs)
如果e.Row.RowType = DataControlRowType.Footer然后
e.Row.Cells(2)。文本=总计:
页脚,显示正在运行的总计
e.Row.Cells(3)。文本= priceTotal.ToString(C)
e.Row.Cells(4)。文本= quantityTotal.ToString(D) e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right
e.Row.Font.Bold = TRUE
万一
结束小组
i am trying to total the price and quantity of all the products added to the gridview and i can't seem to figure out why the total is not showing in the footer. the code that i have for the vb should multiply the quantity with the price and put it into the footer of the gridview. the gridview footer is visible so i know that's not the problem. any help would be appreciated.
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 Public Class 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
Databinding creates data rows. The footer isn't a data row, so the event RowDataBound
is not called when it's created. Thus, your event handler GridView1_RowDataBound
will never execute the code that generates the totals, since the expression
e.Row.RowType = DataControlRowType.Footer
... will never be true during any execution of that method.
Try handling the RowCreated
event instead, like so:
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
这篇关于试图在ASP GridView的总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!