repeater的疑难症

qianlanglearn 2007-11-24 07:41:33
我在网页的左侧做了一个repeater,想用它实现用户浏览过的商品。想实现这样的问题。1.用户点击的商品会在repeater里面显示,只显示头十个,而且最后点击的商品放在最上面,依次类推。2.如果我点击repeater里面的第5个商品,那么第5个商品就放到了第一个位置。
...全文
54 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
大正他爹 2007-11-25
  • 打赏
  • 举报
回复
点击触发一个点击时间,再接时间排序。
drummery 2007-11-25
  • 打赏
  • 举报
回复
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);
}
}
weiphone 2007-11-25
  • 打赏
  • 举报
回复
每次点击 不存在就插入 时间自动 已经存在就更新时间为最新! 点击事件最后面再重新绑定 一下就好了

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧