【发布时间】:2012-01-07 20:33:18
【问题描述】:
我想实现简单的分页。
我目前有一个 Dictionary,并通过使用 foreach 循环遍历它来在页面上显示其内容。
我找不到抵消foreach 循环的方法。
假设我有 100 件商品。每页5个项目,总共20页。我将从以下内容开始:
int counter = 0;
int itemsPerPage = 5;
int totalPages = (items.Count - 1) / itemsPerPage + 1;
int currentPage = (int)Page.Request.QueryString("page"); //assume int parsing here
Dictionary<string, string> currentPageItems = new Dictionary<string, string>;
foreach (KeyValuePair<string, string> item in items) //items = All 100 items
{
//---Offset needed here----
currentPageItems.Add(item.Key, item.Value);
if (counter >= itemsPerPage)
break;
counter++;
}
这将正确输出首页 - 现在如何显示后续页面?
【问题讨论】:
标签: c# dictionary foreach paging