Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用gridview asp.net的shoppingcart_C#_Asp.net_Gridview - Fatal编程技术网

C# 使用gridview asp.net的shoppingcart

C# 使用gridview asp.net的shoppingcart,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在使用asp.net创建购物车 我正在学习我在网上找到的教程。 在添加购物车后,我正在使用gridview查看购物车。 然而, 我不知道为什么,当我添加项目时, 它不会显示在栅格视图中 我做了一个计算,把所有的项目加在一起。 计算确实表明了这一点。只是网格视图中的我的项目似乎没有显示出来 <asp:GridView ID="CartList" runat="server" AutoGenerateColumns="false" ShowFooter=

我正在使用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>
有人能告诉我为什么吗

错误


将页面加载代码放在iPostBack中

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";
        }
    }

}

看起来你在跟踪。尝试将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">

@我在我的代码上做了一个测试来计算总数。我设法显示总成本。但是,问题是,我的项目似乎没有显示在网格视图中。您是否也可以显示您的
CartItem
类的属性?@naveen done,uploaded to one one thing rebind the list CartList.DataBind();
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)
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";
        }
    }

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