我有2页ASP.NET(PageA.aspx和PageB.aspx)。每个页面都可以显示多个记录(大约7000个)。我没有显示所有7000条记录,而是创建了自己的导航类,显示了页码。 (页面#1 =记录1到100;页面#2 =记录101到200等。)。
当PageA加载时,我调用导航类的实例(单例)。这样我就不会失去当前的页面#。问题是,如果我在第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;
}
}
}
答案 0 :(得分:0)
您可能会遇到此方法的其他问题。我能想到的最简单的方案是,当您有多个用户同时访问您的网页时。
为了保持这样的状态,你需要:
我会推荐后者,它更接近无状态的网络形式。