如何在数据列表或网格视图中绑定和显示集合

时间:2015-03-12 00:32:26

标签: c# asp.net data-binding listbox

我的页面有三个课程。产品显示在页面上并添加到购物篮中。

添加的产品通过列表框显示在列表框中。

我无法弄清楚如何在数据列表或网格视图中绑定和显示集合,因此我可以更好地控制布局和我可能要添加的任何其他功能。例如添加删除项目等。

以下是课程:

public class Product
{
    public string ProductID { get; set; }
    public string Name { get; set; }
    public string ShortDescription { get; set; }
    public string LongDescription { get; set; }
    public decimal UnitPrice { get; set; }
    public string ImageFile { get; set; }
}

public class CartItem
{
    public CartItem() {}

    public CartItem(Product product, int quantity)
    {
        this.Product = product;
        this.Quantity = quantity;
    }

    public Product Product { get; set; }
    public int Quantity { get; set; }

    public void AddQuantity(int quantity)
    {
        this.Quantity += quantity;
    }

    public string Display()
    {
        string displayString = 
            Product.Name + " (" + Quantity.ToString()
            + " at " + Product.UnitPrice.ToString("c") + " each)";
        return displayString;
    }
}


public class CartItemList
{
    private List<CartItem> cartItems;

    public CartItemList()
    {
        cartItems = new List<CartItem>();
    }

    public int Count { get { return cartItems.Count; } }

    public CartItem this[int index]
    {
        get { return cartItems[index]; }
        set { cartItems[index] = value; }
    }

    public CartItem this[string id]
    {
        get {
            foreach (CartItem c in cartItems)
                if (c.Product.ProductID == id) return c;
            return null;
        }
    }

    public static CartItemList GetCart()
    {
        CartItemList cart = (CartItemList) HttpContext.Current.Session["Cart"];
        if (cart == null)
            HttpContext.Current.Session["Cart"] = new CartItemList();
        return (CartItemList) HttpContext.Current.Session["Cart"];
    }

    public void AddItem(Product product, int quantity)
    {
        CartItem c = new CartItem(product, quantity);
        cartItems.Add(c);
    }

    public void RemoveAt(int index)
    {
        cartItems.RemoveAt(index);
    }

    public void Clear()
    {
        cartItems.Clear();
    }
}

在我的页面上,产品显示在数据库的数据列表中。绑定是通过sqldatasource完成的。

这是add方法,它将产品添加到列表框中,只需添加并显示从类中的display()方法返回的字符串。 我想知道如何在标签控件等项目中显示项目。

protected void add(object sender, EventArgs e)
{
    Product p = new Product();
    LinkButton BuyNowButton = (LinkButton)sender;
    DataListItem item = (DataListItem)BuyNowButton.NamingContainer;
    Label IdLabel = (Label)item.FindControl("productidLabel");
    Label NameLabel = (Label)item.FindControl("productnameLabel");
    Label ValueLabel = (Label)item.FindControl("productpriceLabel");
    p.ProductID = IdLabel.Text;
    p.ProductName = NameLabel.Text;
    p.ProductPrice =Convert.ToDecimal(ValueLabel.Text);

    CartItemList cart = CartItemList.GetCart();
    CartItem cartItem = cart[p.ProductID];
    //if item isn’t in cart, add it; otherwise, increase its quantity
    TextBox tb = (TextBox)item.FindControl("TextBoxQuantity");
    int quantity = Convert.ToInt16(tb.Text);
    if (cartItem == null)
    {
        cart.AddItem(p,quantity);
        total += Math.Round(p.ProductPrice * quantity, 2);
    }
    else
    {
        cartItem.AddQuantity(quantity);
        total += Math.Round(p.ProductPrice * quantity, 2);
    }

    displayList();
}

以下是显示购物篮的方法:

protected void displayList()
{
    CartItemList cart = CartItemList.GetCart();
    CartItem item;

    for (int i = 0; i < cart.Count; i++)
    {
        item = cart[i];
        ListBox1.Items.Add(item.Display());
    }

    Label3.Text = "Total : " + total.ToString();
}

如何以一种非常可定制的方式显示来自数据库或类似网页上的arraylist的数据,如添加&#34; addquantity&#34; &#34;除去&#34; &#34;一旦订单总额超过一定金额等显示消息等。&#34;?

到目前为止,我只是将它绑定到gidviews或数据列表或列表框,但这并不能让我以我想要的方式呈现它。

在此处{@ 3}} https://takeaway.nandos.co.uk/Menu/Appeteasers

等网站购物篮订购系统的幕后发生了什么?

0 个答案:

没有答案
相关问题