我正在使用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)
看来您正在按照本教程进行操作。尝试在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}" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句