c# - System.notSupportedException'TemplateField'

时间:2017-06-28 01:35:30

标签: c# asp.net

我正在尝试修复我的代码,因为有错误,'不支持回调'

  

System.NotSupportedException发生Message =回调不是   由于某些控件无法更新,因此支持TemplateField   正确地回调。在'gvShoppingCart'上关闭回调。

这是我对设计源的编码,ShoppingCart.aspx

<asp:GridView ID="gvShoppingCart" runat="server" AutoGenerateColumns="False" EnablePersistedSelection="True" EnableSortingAndPagingCallbacks="True" Height="178px" OnRowCancelingEdit="gvShoppingCart_RowCancelingEdit" OnRowDeleting="gvShoppingCart_RowDeleting" OnRowEditing="gvShoppingCart_RowEditing" OnRowUpdating="gvShoppingCart_RowUpdating" Width="614px">
    <Columns>
        <asp:BoundField DataField="ProductName" HeaderText="Product Name" ReadOnly="True" />
        <asp:ImageField DataImageUrlField="Image" DataImageUrlFormatString="~\Images\{0}" HeaderText="Image" ReadOnly="True">
        </asp:ImageField>
        <asp:BoundField DataField="Price" HeaderText="Price" ReadOnly="True" />
        <asp:BoundField DataField="Brand" HeaderText="Brand" ReadOnly="True" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Double.Parse(Eval("Price").ToString())*Int32.Parse(Eval("Quantity").ToString()) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
</asp:GridView>

这是发生错误的ShoppingCart.aspx.cs文件。

Cart myCart;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["myCart"] == null)
            {
                Session["myCart"] = new Cart();
            }
            myCart =(Cart)Session["myCart"];
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            gvShoppingCart.DataSource = myCart.Items;
            gvShoppingCart.DataBind(); //error comes up here
            if (myCart.Items.Count ==0)
            {
                lblGrandTotal.Visible = false;
            }
            else
            {
                lblGrandTotal.Text = string.Format("Grand Total = {0,19:C}", myCart.GrandTotal);
                lblGrandTotal.Visible = true;
            }
        }

我将EnableSortingAndPagingCallbacks从'True'更改为'False'后出现了新的错误

  

System.InvalidOperationException:'必须为其指定DataKeyNames   坚持选择工作。'

错误发生在与上一个错误相同的代码中。是的,

private void FillData()
        {
            gvShoppingCart.DataSource = myCart.Items;
            gvShoppingCart.DataBind();
            if (myCart.Items.Count ==0)

这是我的CartItem.cs,类文件。     公共类CartItem     {         private int v1;         私人字符串v2;         私人字符串v3;         私人双v4;         私人字符串v5;         私人字符串v;

    public int Id { get; set; }
    public string ProductName { get; set;}
    public string Image { get; set; }
    public double Price { get; set; }
    public string Brand { get; set; }
    public int Quantity { get; set; }

    public CartItem()
    {

    }

    public CartItem(int Id, string ProductName, string Image, double Price, string Brand, int Quantity)
    {
        this.Id = Id;
        this.ProductName = ProductName;
        this.Image = Image;
        this.Price = Price;
        this.Brand = Brand;
        this.Quantity = Quantity;
    }

    public CartItem(int v1, string v2, string v3, double v4, string v5)
    {
        this.v1 = v1;
        this.v2 = v2;
        this.v3 = v3;
        this.v4 = v4;
        this.v5 = v5;
    }

    public CartItem(int v1, string v2, string v3, string v)
    {
        this.v1 = v1;
        this.v2 = v2;
        this.v3 = v3;
        this.v = v;
    }
}

0 个答案:

没有答案