using System;
using System.Text;
using Manu.Web.UI.WebControls;
using UrlRewritingNet.Web;
namespace Manu.Web.UI.WebControls
{
public class
UrlRewritingPager : PagerDataSource
{
public
override string GenerateUrl(int
destinationPage)
{
// in design mode,
delegate to the parent class
if (DesignMode)
{
return
base.GenerateUrl(destinationPage);
}
string
urlPath, page;
// get the current
URL path and the current page
GetUrl(out
urlPath, out page);
// creates a string
builder to build the fake directory structure
StringBuilder
sb = new StringBuilder();
sb.Append(urlPath);
foreach
(string key in
Context.Request.QueryString.Keys) {
//
add the parameter if it's not the paging parameter
if
(String.Compare(key, PageKey) != 0) {
sb.Append(key);
sb.Append('/');
sb.Append(Context.Request.QueryString[key]);
sb.Append('/');
}
}
// adds the paging
parameter
sb.Append(PageKey);
sb.Append('/');
sb.Append(destinationPage);
sb.Append('/');
// adds the page
sb.Append(page);
return
sb.ToString();
}
public
void GetUrl(out string
urlPath, out string
page)
{
// gets the URL
of the request
string
url = Context.Request.Url.AbsoluteUri;
// if the path
has been rewriten, get the real path
if (Context.Items[UrlRewriteModule.PhysicalPath] !=
null) {
url = (string)Context.Items[UrlRewriteModule.PhysicalPath];
}
// deletes the
query string from the url
int queryCharIndex
= url.IndexOf('?');
if (queryCharIndex
!= -1) {
url = url.Substring(0, queryCharIndex);
}
// extracts the
page from the url
int lastSlash
= url.LastIndexOf('/');
page = url.Substring(lastSlash + 1);
// delete the page
from the url
urlPath = url.Substring(0, lastSlash +
1);
}
}
}