Navigation links, typically, have a whole series of key-value pairs in its query string. And, typically, one of them would be the page key (e.g. page=3). This is the marker for which page does the current content belong to. There might be additional key-value pairs that denote the category, sub-category, the article name, customer name, etc.
The navigation links would typically give the user an opportunity to move backwards or forwards. Meaning, if the user is currently on Page 3, the backward link would point to Page 2 and the forward link would point to Page 4. All you would need to do is to replace the page key-value pair in the querystring without disturbing others.
The following code shows one way of doing that (in the example below, you will have the numItems, i.e. the total items from, for example, a database query. Similarly, you will have some kind of pageSize, either predefined or user-set):
// To begin with, enable both the Previous and Next buttons
lnkPrevious.Enabled = true;
lnkNext.Enabled = true;
// Current Page (reqPage)
int reqPage;
if (Request.QueryString["Page"] != null)
reqPage = Convert.ToInt16(Request.QueryString["Page"]);
else
reqPage = 1;
// Check the cases where Next or Previous should not be enabled.
// If this is the last page, there is no Next
if (numItems <= reqPage * pageSize)
lnkNext.Enabled = false;
// If this is the first page, there is no Previous
if (reqPage < 2)
lnkPrevious.Enabled = false;
// Make up the next and previous query strings
StringBuilder nextQueryString = new StringBuilder();
StringBuilder prevQueryString = new StringBuilder();
bool pageKeyExists = false;
bool isFirstKey = true;
foreach (string key in Request.QueryString.AllKeys)
{
// We don’t want to put & infront of the first key in the querystring
if (!isFirstKey)
{
nextQueryString.Append("&");
prevQueryString.Append("&");
}
// Increase (for Next) or Decrease (for Previous) the page number
if (key == "Page")
{
nextQueryString.AppendFormat("Page={0}", reqPage + 1);
prevQueryString.AppendFormat("Page={0}", reqPage - 1);
pageKeyExists = true;
}
// If it’s not page key, don’t disturb it
else
{
nextQueryString.AppendFormat("{0}={1}", key, Request.QueryString[key]);
prevQueryString.AppendFormat("{0}={1}", key, Request.QueryString[key]);
}
isFirstKey = false;
}
// If the Page key does not exist in the query string, the values shown
// belong to Page 1. We need to now go to the second page.
if (!pageKeyExists)
{
// If this is the first key, don’t put an & in front of the key.
if (Request.QueryString.Keys.Count > 0)
{
nextQueryString.Append("&");
prevQueryString.Append("&");
}
// Set the Next to Page 2 and Disable the previous button
nextQueryString.Append("Page=2");
lnkPrevious.Enabled = false;
}
// Makeup the URLs for the navigation buttons.
lnkPrevious.NavigateUrl = "~/ItemListing.aspx?" + prevQueryString.ToString();
lnkNext.NavigateUrl = "~/ItemListing.aspx?" + nextQueryString.ToString();
In the above code, we have:
- Obtained the current page number (reqPage)
- Didn’t put & in front of the first key-value pair
- Adjusted the page numbers for different links
- Didn’t change the non-page key-value pairs
- Took care of the case where there is no page key in the querystring
- Enabled/Disabled the Next/Previous link according to the current page.