我的Shopping.cs文件在下面给出
using System;
using System.Data;
using MyWebsite.Commerce;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace MyWebsite.Commerce
{
[Serializable]
public class CartItem
{
private int _productID;
private string _productName;
private string _productURL;
private int _quantity;
private double _price;
private double _lineTotal;
public void New()
{
}
public void New(int ProductID, string ProductName, string ProductURL, int Quantity, double Price)
{
_productID = ProductID;
_productName = ProductName;
_productURL = ProductURL;
_quantity = Quantity;
_price = Price;
_lineTotal = Quantity * Price;
}
public int ProductID { get; set; }
public string ProductName { get; set; }
public string ProductURL { get; set; }
public int Quantity { get; set; }
public double Price { get; set; }
public double LineTotal { get; set; }
[Serializable]
public class ShoppingCart
{
private DateTime _dateCreated;
private DateTime _lastUpdate;
private List<CartItem> _items;
public ShoppingCart()
{
if (this._items = null)
{
this._items = new List<CartItem>();
this._dateCreated = DateTime.Now;
}
}
public List<CartItem> Items
{ get; set; }
public void Insert(int ProductID, string ProductName, string ProductURL, int Quantity, double Price)
{
int ItemIndex = ItemIndexOfID(ProductID);
if (ItemIndex == -1)
{
CartItem NewItem = new CartItem();
NewItem.ProductID = ProductID;
NewItem.ProductName = ProductName;
NewItem.ProductURL = ProductURL;
NewItem.Price = Price;
NewItem.Quantity = Quantity;
_items.Add(NewItem);
}
else
{
_items[ItemIndex].Quantity += 1;
}
_lastUpdate = DateTime.Now;
}
public void Update(int RowID, int ProductID, int Quantity, double Price)
{
CartItem Item = _items[RowID];
Item.ProductID = ProductID;
Item.Quantity = Quantity;
Item.Price = Price;
_lastUpdate = DateTime.Now;
}
public void DeleteItem(int rowID)
{
_items.RemoveAt(rowID);
_lastUpdate = DateTime.Now;
}
private int ItemIndexOfID(int ProductID)
{
int index = 0;
foreach (CartItem item in _items)
{
if (item.ProductID == ProductID)
{
return index;
}
index += 1;
}
return -1;
}
public double Total
{
get
{
double t = 0;
if (_items == null)
{
return 0;
}
foreach (CartItem Item in _items)
{
t += Item.LineTotal;
}
return t;
}
}
}
}
}
and then i create a web user control file ShoppingCart.ascx and ShoppingCart.ascx.cs file code are given below of ShoppingCart.ascx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyWebsite.Commerce;
public partial class ShoppingCart : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Profile.Cart == null)
{
Profile.Cart = new MyWebsite.Commerce.ShoppingCart();
}
if (!Page.IsPostBack)
{
BindGrid();
}
if (Profile.Cart.Items == null)
{
TotalLabel.Visible = false;
}
}
protected void CartGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
CartGrid.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void CartGrid_RowUpdating(object sender, GridViewUpdatedEventArgs e)
{
TextBox QuantityTextBox = (TextBox)CartGrid.Rows[e.RowIndex].Cells[2].Controls[0];
int Quantity = Convert.ToString(QuantityTextBox.Text);
if (Quantity == 0)
{
Profile.Cart.Items.RemoveAt(e.RowIndaex);
}
else
{
Profile.Cart.Items[e.RowIndex].Quantity = Quantity;
}
CartGrid.EditIndex = -1;
BindGrid();
}
protected void CartGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
CartGrid.EditIndex = -1;
BindGrid();
}
protected void CartGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Profile.Cart.Items.RemoveAt(e.RowIndex);
BindGrid();
}
private void BindGrid()
{
CartGrid.DataSource = Profile.Cart.Items;
DataBind();
TotalLabel.Text = string.Format("Total : {0,19:c}", Profile.Cart.Total);
}
}
当我将ShoppingCart.ascx文件拖放到ShoppingCartPage.aspx文件并运行应用程序时发生错误,当我点击错误时我继续下面的代码
描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并适当修改源代码。
编译器错误消息:CS0234: 类型或命名空间名称'ShoppingCart' 在命名空间中不存在 'MyWebsite.Commerce'(你错过了吗? 汇编参考?)
第17行:公共类ProfileCommon: System.Web.Profile.ProfileBase {Line 18:第19行:公共虚拟 MyWebsite.Commerce.ShoppingCart Cart { 第20行:获得{第21行:第 返回 ((MyWebsite.Commerce.ShoppingCart)(this.GetPropertyValue( “购物车”)));
请帮助我..................
答案 0 :(得分:0)
不是说这是解决此问题的最佳或唯一方法,但我会尝试删除所有引用并逐个添加它们,直到您重新开始工作..
答案 1 :(得分:0)
这对我来说很难掌握,所以我只想在这里提出一个想法供你测试。因为我看到你的班级
你有
namespace MyWebsite.Commerce
然后你有一个CartItem类。然后,不是拥有另一个完全独立的类ShoppingCart,而是在CartItem中拥有该类。
我可能是错的(因为我没有尝试过测试),但我认为你的ascx中有一行
Profile.Cart = new MyWebsite.Commerce.ShoppingCart();
应该是
Profile.Cart = new MyWebsite.Commerce.CartItem.ShoppingCart();
注意额外的CartItem。
或者将ShoppingCart类编写为MyWebsite.Commerce命名空间中的单独类。