问题描述
我使用asp.net创建一个购物车
我下面,我已经在网上找到的教程。
我使用的GridView可以查看我在加入后,购物车。
然而,
我不知道为什么,当我增加一个项目,
它不会在网格视图中显示。
我做了一个计算,将所有的物品一起。
计算显示出来。这只是我的网格视图项不似乎显示。
< ASP:GridView控件ID =CartList=服务器
的AutoGenerateColumns =假
ShowFooter =真
网格线=垂直
CELLPADDING =4
而itemtype =MyWebStore.Models.CartItem
SelectedMethod =GetShoppingCartItems
的CssClass =table表条纹表镶上>
<柱体和GT;
< ASP:BoundField的数据字段=产品的HeaderText =IDSORTEX pression =产品/>
< ASP:BoundField的数据字段=Products.ProductName的HeaderText =名称/>
< ASP:BoundField的数据字段=Products.UnitPrice的HeaderText =价格(每个)DataFormatString ={0:C}/>
< ASP:的TemplateField的HeaderText =数量>
<&ItemTemplate中GT;
< ASP:文本框ID =PurchaseQuantity=服务器WIDTH =40文本=<%#:Item.Quantity%>>
< / ASP:文本框>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =项目总>
<&ItemTemplate中GT;
<%#:的String.Format({0:C}((Convert.ToDouble(Item.Quantity))* Convert.ToDouble(Item.Product.UnitPrice)))%GT;
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =删除项目>
<&ItemTemplate中GT;
< ASP:复选框ID =删除=服务器>< / ASP:复选框>
< / ItemTemplate中>
< / ASP:的TemplateField>
< /专栏>
< / ASP:GridView的>
code-背后
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
使用(ShoppingCartActions usersShoppingCart =新ShoppingCartActions())
{
小数cartTotal = 0;
cartTotal = usersShoppingCart.GetTotal();
如果(cartTotal大于0)
{
//显示总
lblTotal.Text =的String.Format({0:C},cartTotal);
}
其他
{
LabelTotalText.Text =;
lblTotal.Text =;
ShoppingCartTitle.InnerText =购物车是空的;
}
}
}公开名单< CartItem> GetShoppingCartItems()
{
ShoppingCartActions行动=新ShoppingCartActions();
返回actions.GetCartItems();
}保护无效CartList_SelectedIndexChanged(对象发件人,EventArgs的发送)
{}
CartItem类
公共部分类CartItem
{
公共字符串条目标识{搞定;组; }
公共字符串CartID {搞定;组; }
公众诠释数量{搞定;组; }
公众的System.DateTime dateCreated会获得{;组; }
公众诠释的ProductID {搞定;组; } 公共虚拟产品产品{搞定;组; }
}
有人能告诉我为什么?
错误
System.Web.UI.DataBinder.GetPropertyValue(对象容器,字符串作为propName)在System.Web.UI.DataBinder.Eval(对象容器,字符串[] EX pressionParts )留在System.Web.UI.WebControls.BoundField.GetValue(控制controlContainer)在System.Web.UI.WebControls.BoundField.OnDataBindField System.Web.UI.DataBinder.Eval(对象容器,字符串前pression) (对象发件人,EventArgs e)上System.Web.UI.Control.OnDataBinding(EventArgs e)上System.Web.UI.Control.DataBind(布尔raiseOnDataBinding)在System.Web.UI.Control.DataBind()的系统。 Web.UI.Control.DataBindChildren()在System.Web.UI.Control.DataBind(布尔raiseOnDataBinding)在System.Web.UI.Control.DataBind()在System.Web.UI.WebControls.GridView.CreateRow(的Int32的rowIndex ,的Int32 dataSourceIndex,DataControlRowType ROWTYPE,DataControlRowState的RowState,布尔数据绑定,对象的DataItem,的DataControlField []在System.Web程序领域,TableRowCollection行,pagedDataSource pagedDataSource)在System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable的数据源,数据绑定布尔)在System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable的数据)在System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable的数据).UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable的数据)在System.Web.UI程序.DataSourceView.Select(DataSourceSelectArguments参数,DataSourceViewSelectCallback回调)在System.Web.UI.WebControls.DataBoundControl.PerformSelect()在System.Web.UI.WebControls.BaseDataBoundControl.DataBind()在System.Web.UI.WebControls.GridView。的DataBind()在System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()在System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()在System.Web.UI.Control.EnsureChildControls()在System.Web.UI程序。控制。preRenderRecursiveInternal()留在System.Web.UI.Control。$ p $在System.Web.UI.Control。preRenderRecursiveInternal()System.Web.UI.Control。preRenderRecursiveInternal() pRenderRecursiveInternal()在System.Web.UI.Control。preRenderRecursiveInternal()在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
你看上去如下的本教程。尝试改变 SelectedMethod =GetShoppingCartItems
到 SelectMethod =GetShoppingCartItems
在ASPX code
< ASP:GridView控件ID =CartList=服务器
的AutoGenerateColumns =假
ShowFooter =真
网格线=垂直
CELLPADDING =4
而itemtype =MyWebStore.Models.CartItem
SelectMethod =GetShoppingCartItems
的CssClass =table表条纹表镶上>
和以下似乎是错误的:
< ASP:BoundField的数据字段=Products.ProductName的HeaderText =名称/>
< ASP:BoundField的数据字段=Products.UnitPrice的HeaderText =价格(每个)DataFormatString ={0:C}/>
您绑定列表< CartItem>
到 CartList
,但 CartItem
类没有名为任何属性产品
。尝试这两行改成这样:
< ASP:BoundField的数据字段=Product.ProductName的HeaderText =名称/>
< ASP:BoundField的数据字段=Product.UnitPrice的HeaderText =价格(每个)DataFormatString ={0:C}/>
I am creating a shopping cart using asp.net
I following a tutorial that I've found online.I'm using gridview to view the cart after I've added it in.However,I don't know why, when I add a item,it doesn't show in the grid view.
I did a calculation, adding all the items together.The calculation does show. It's just that my item in the grid view doesn't seems to be showing.
<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>
Code-behind
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 class
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; }
}
Can someone tell me why?
error
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)
Looks like you're following this tutorial. Try to change SelectedMethod="GetShoppingCartItems"
to SelectMethod="GetShoppingCartItems"
in the aspx code
<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">
and the following seems wrong:
<asp:BoundField DataField="Products.ProductName" HeaderText="Name" />
<asp:BoundField DataField="Products.UnitPrice" HeaderText="Price (each)" DataFormatString="{0:c}" />
You bind a List<CartItem>
into CartList
, but CartItem
class doesn't have any properties named Products
. Try to change those two lines to this:
<asp:BoundField DataField="Product.ProductName" HeaderText="Name" />
<asp:BoundField DataField="Product.UnitPrice" HeaderText="Price (each)" DataFormatString="{0:c}" />
这篇关于使用购物车的GridView asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!