shoppingcart使用gridview asp.net

时间:2014-02-01 14:24:11

标签: c# asp.net gridview

我正在使用asp.net创建购物车

我按照我在网上找到的教程。 在我添加它之后,我正在使用gridview查看购物车。 然而, 我不知道为什么,当我添加一个项目时, 它不会显示在网格视图中。

我做了一个计算,将所有项目加在一起。 计算确实显示出来。只是网格视图中的项目似乎没有显示。

<asp:GridView ID="CartList" runat="server" 
        AutoGenerateColumns="false" 
        ShowFooter="True"
        GridLines="Vertical" 
        CellPadding="4" 
        ItemType="MyWebStore.Models.CartItem" 
        SelectedMethod="GetShoppingCartItems"
        CssClass="table table-striped table-bordered">
    <Columns>
        <asp:BoundField DataField="ProductID" HeaderText="ID" SortExpression="ProductID" />
        <asp:BoundField DataField="Products.ProductName" HeaderText="Name" />
        <asp:BoundField DataField="Products.UnitPrice" HeaderText="Price (each)" DataFormatString="{0:c}" />
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="PurchaseQuantity" runat="server" Width="40" Text="<%#: Item.Quantity %>">
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item Total">
            <ItemTemplate>
                <%#: String.Format("{0:c}",((Convert.ToDouble(Item.Quantity)) * Convert.ToDouble(Item.Product.UnitPrice))) %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Remove Item">
            <ItemTemplate>
                <asp:CheckBox ID="Remove" runat="server"></asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏

protected void Page_Load(object sender, EventArgs e)
{
    using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
    {
        decimal cartTotal = 0;
        cartTotal = usersShoppingCart.GetTotal();
        if (cartTotal > 0)
        {
            //Display total
            lblTotal.Text = String.Format("{0:c}", cartTotal);
        }
        else
        {
            LabelTotalText.Text = "";
            lblTotal.Text = "";
            ShoppingCartTitle.InnerText = "Shopping Cart is Empty";
        }
    }
}

public List<CartItem> GetShoppingCartItems()
{
    ShoppingCartActions actions = new ShoppingCartActions();
    return actions.GetCartItems();
}

protected void CartList_SelectedIndexChanged(object sender, EventArgs e)
{

}

CartItem类

public partial class CartItem
{
    public string ItemID { get; set; }
    public string CartID { get; set; }
    public int Quantity { get; set; }
    public System.DateTime DateCreated { get; set; }
    public int ProductID { get; set; }

    public virtual Product Product { get; set; }
}

有人可以告诉我为什么吗?

错误

System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) at System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts) at System.Web.UI.DataBinder.Eval(Object container, String expression) at System.Web.UI.WebControls.BoundField.GetValue(Control controlContainer) at System.Web.UI.WebControls.BoundField.OnDataBindField(Object sender, EventArgs e) at System.Web.UI.Control.OnDataBinding(EventArgs e) at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind() at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) at System.Web.UI.WebControls.DataBoundControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at System.Web.UI.WebControls.GridView.DataBind() at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() at System.Web.UI.Control.EnsureChildControls() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

2 个答案:

答案 0 :(得分:1)

看起来你正在关注this tutorial。尝试在aspx代码中将SelectedMethod="GetShoppingCartItems"更改为SelectMethod="GetShoppingCartItems"

<asp:GridView ID="CartList" runat="server" 
        AutoGenerateColumns="false" 
        ShowFooter="True"
        GridLines="Vertical" 
        CellPadding="4" 
        ItemType="MyWebStore.Models.CartItem" 
        SelectMethod="GetShoppingCartItems"
        CssClass="table table-striped table-bordered">

以下似乎错了:

<asp:BoundField DataField="Products.ProductName" HeaderText="Name" />
<asp:BoundField DataField="Products.UnitPrice" HeaderText="Price (each)" DataFormatString="{0:c}" />

您将List<CartItem>绑定到CartList,但CartItem类没有任何名为Products的属性。尝试将这两行更改为:

<asp:BoundField DataField="Product.ProductName" HeaderText="Name" />
<asp:BoundField DataField="Product.UnitPrice" HeaderText="Price (each)" DataFormatString="{0:c}" />

答案 1 :(得分:0)

将Page_Load代码放在IsPostBack

if(!IsPostBack)
{
using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
    {
        decimal cartTotal = 0;
        cartTotal = usersShoppingCart.GetTotal();
        if (cartTotal > 0)
        {
            //Display total
            lblTotal.Text = String.Format("{0:c}", cartTotal);
        }
        else
        {
            LabelTotalText.Text = "";
            lblTotal.Text = "";
            ShoppingCartTitle.InnerText = "Shopping Cart is Empty";
        }
    }

}