質問

I got 2 pages ASP.NET (PageA.aspx and PageB.aspx). Each Page can display muliple records (about 7000). Instead of showing all 7000 records, I created my own navigation class that display the page number. (Page#1 = record 1 to 100; Page#2 = record 101 to 200 etc..).

When the PageA loads, I call an instance (singleton) of my navigation class. This way I'm not loosing the current Page#. The problem is that if I'm at page #6 and move to the PageB.aspx, because I created an instance, PageB.aspx start at Page#6. I'd like to set the current page to 1 in Page_Load, but as I can see, Page_Load is called evrytime something change in my page :(

What's the best way of doing this?

Here's my PageA.aspx as example

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]);
    }
}

Here's the Navigation class

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;
        }
    }
}
役に立ちましたか?

解決

You are likely to run into other problems with this approach. The simplest scenario that I can think of would be when you have more than one user accessing your pages at one time.

In order to keep state like this, you need to either:

  • Store your navigation state in the ViewState (using the Session would cause the same problem across multiple pages for the same user)
  • Place the current page in a hidden field so that the browser sends it back on each request

I would recommend the latter, it is closer to the stateless form of the web.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top