我在网页的左侧做了一个repeater,想用它实现用户浏览过的商品。想实现这样的问题。1.用户点击的商品会在repeater里面显示,只显示头十个,而且最后点击的商品放在最上面,依次类推。2.如果我点击repeater里面的第5个商品,那么第5个商品就放到了第一个位置。

解决方案 »

  1.   

    lz试下这段代码,基本思路是建立一个队列类,定义两个基本方法:Push() 和SetTop(), 并在Session中缓存这个队列类。
    前台    <form id="form1" runat="server">
        <div>
       <asp:Repeater ID="rptCart" runat="server" OnItemCommand="rptCart_ItemCommand">
       <HeaderTemplate><ul></HeaderTemplate>
       <ItemTemplate>
       <li><asp:LinkButton ID="btnProuduct" runat="server" CommandName="SetTop" Text='<%# Eval("Name") %>' /></li>
       </ItemTemplate>
       <FooterTemplate></ul></FooterTemplate>
       </asp:Repeater>
       <asp:TextBox ID="editProduct" runat="server" />
       <asp:Button ID="btnAdd" runat="server" Text="Add product to cart." OnClick="btnAdd_Click" /> 
        
        </div>
        </form>后台public partial class Default2 : System.Web.UI.Page
    {
        private CProductQueue _queue = null;
        /// <summary>
        /// Use session to record the custmolized items.
        /// </summary>
        protected CProductQueue Queue
        {
            get
            {
                if (Session["Queue"] == null)
                {
                    _queue = new CProductQueue(3);
                    Session["Queue"] = _queue;
                }
                else
                {
                    _queue = Session["Queue"] as CProductQueue;
                }
                return _queue;
            }
        }    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindRepeater();
            }
        }    protected void btnAdd_Click(object sender, EventArgs e)
        {
            Queue.Push(new CProduct(editProduct.Text.Trim()));
            BindRepeater();
        }    protected void rptCart_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "SetTop":
                    Queue.SetTop(e.Item.ItemIndex);
                    break;
            }
            BindRepeater();
        }    private void BindRepeater()
        {
            rptCart.DataSource = Queue.Products;
            rptCart.DataBind();
        }
    }public class CProduct
    {
        private string _name = "";    public string Name
        {
            get { return _name; }
            set { _name = value; }
        }    public CProduct(string name)
        {
            _name = name;
        }    public override string ToString()
        {
            return string.Format("Product: Name={1}", _name);
        }
    }public class CProductQueue
    {
        private ArrayList listProducts = null;
        private int _maxLength;    public CProductQueue(int maxLength)
        {
            listProducts = new ArrayList();
            _maxLength = maxLength;
        }
        public CProductQueue()
        {
            _maxLength = 10;
        }    public CProduct this[int index]
        {
            get { return IsIndexValid(index) ? listProducts[index] as CProduct : null ; }
            set 
            {
                if (IsIndexValid(index))
                {
                    listProducts[index] = value;
                }
            }
        }    public ArrayList Products
        {
            get
            {
                return listProducts;
            }
        }    public void Push(CProduct prodcut)
        {
            // Inset at first position.
            listProducts.Insert(0, prodcut);
            if (listProducts.Count > _maxLength)
            {
                // Remove the last one if the length exceeds.
                listProducts.RemoveAt(_maxLength);
            }
        }    /// <summary>
        /// Sets the product with the specific index top of the query.
        /// </summary>
        public void SetTop(int index)
        {
            if (IsIndexValid(index) && index > 0)
            {
                CProduct origin = listProducts[index] as CProduct;
                CProduct product = new CProduct(origin.Name);
                listProducts.RemoveAt(index);
                Push(product);
            }
        }    private bool IsIndexValid(int index)
        {
            return (index < listProducts.Count && index >= 0);
        }
    }