Reputation: 149
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)
Upvotes: 1
Views: 4139
Reputation: 14614
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}" />
Upvotes: 1
Reputation: 28652
Place your Page_Load code inside 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";
}
}
}
Upvotes: 0