我有 2 页 ASP.NET(PageA.aspx 和 PageB.aspx)。每个 Page 可以显示多条记录(约 7000 条)。我没有显示所有 7000 条记录,而是创建了自己的导航类来显示页码。(第 1 页 = 记录 1 到 100;第 2 页 = 记录 101 到 200 等等。)。
当 PageA 加载时,我调用导航类的一个实例(单例)。这样我就不会丢失当前的 Page#。问题是如果我在第 6 页并移动到 PageB.aspx,因为我创建了一个实例,PageB.aspx 从第 6 页开始。我想在 Page_Load 中将当前页面设置为 1,但正如我所见,Page_Load 被称为 evrytime 我的页面发生了一些变化:(
这样做的最佳方法是什么?
这是我的 PageA.aspx 作为示例
protected void Page_Load(object sender, EventArgs e)
{
navigation = NavigationPage.getInstance();
loadItems();
}
private void loadItems()
{
ArrayList arItems = soldJobsDAO.GetQuotesToShow();
loadPageNavigation(arItems.Count);
// Start Table Headers
// ...
// End Table Headers
int min = navigation.minPage > arItems.Count ? 0 : navigation.minPage;
int max = navigation.maxPage > arItems.Count ? arItems.Count : navigation.maxPage;
for (int i = min; i < max; i++)
{
SoldJobs job = (SoldJobs)arItems[i];
// Start Table Rows
// Start Table Cells
// End Table Cells
// End Table Rows
}
}
private void loadPageNavigation(int maxValue)
{
PageNavigation.Controls.Clear();
ArrayList NavDiv = navigation.getNavigationPage(maxValue);
for(int i = 0; i < NavDiv.Count; i++)
{
PageNavigation.Controls.Add((LinkButton)NavDiv[i]);
}
}
这是导航类
namespace HomeSite.Class
{
public class NavigationPage
{
private static NavigationPage navigation;
private int m_currentPage;
private int m_minPage;
private int m_maxPage;
private const int NB_ITEM_PER_PAGE = 15;
private NavigationPage()
{
currentPage = 1;
}
public static NavigationPage getInstance()
{
if (navigation == null)
{
navigation = new NavigationPage();
}
return navigation;
}
public int currentPage
{
get
{
return m_currentPage;
}
set
{
m_currentPage = value;
maxPage = currentPage * NB_ITEM_PER_PAGE;
minPage = maxPage - NB_ITEM_PER_PAGE;
}
}
public int minPage
{
get
{
return m_minPage;
}
set
{
m_minPage = value;
}
}
public int maxPage
{
get
{
return m_maxPage;
}
set
{
m_maxPage = value;
}
}
public int getNumberItemPerPages
{
get
{
return NB_ITEM_PER_PAGE;
}
}
public ArrayList getNavigationPage(int maxValue)
{
ArrayList arItems = new ArrayList();
int maxNbPagesToDisplay = 11;
int nbPages = 1;
if (maxValue > 0)
nbPages = maxValue / getNumberItemPerPages + 1;
if (nbPages != 1)
{
LinkButton link;
// We want the current page to be centered if possible
int FirstPageDisplayed = currentPage - (maxNbPagesToDisplay/2);
if (FirstPageDisplayed < 1)
FirstPageDisplayed = 1;
int LastPageDisplayed = FirstPageDisplayed + maxNbPagesToDisplay - 1;
if (LastPageDisplayed > nbPages)
LastPageDisplayed = nbPages;
for (int i = (FirstPageDisplayed - 1); i <= (LastPageDisplayed - 1); i++)
{
int PageNumber = i + 1;
link = new LinkButton();
link.ID = "Page" + PageNumber.ToString();
link.Text = PageNumber.ToString();
link.CommandArgument = PageNumber.ToString();
//link.Command += LinkButton_SetCurrentPage;
link.Style.Add("font-size", "14px");
arItems.Add(link);
}
}
return arItems;
}
}
}