public class PageList : CompositeControl
{
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Span;
}
}
public PageList(int pageIndex, int pageSize, int maxPageCount, int count): base()
{
//page count
int pageCount = count / pageSize;
if (count % pageSize != 0) pageCount++;
//reset pageIndex
if(pageIndex <0)pageIndex =0;
if(pageIndex >pageCount )pageIndex = pageCount ;
//url
string url = Favor.CurrentPage.Request.RawUrl;
if (url.Contains("?")) url.Remove(url.LastIndexOf('?'));
url += "?pageIndex=";
//startIndex,endIndex
int startIndex = 0, endIndex = 0;
GetPageArea(ref startIndex, ref endIndex, pageIndex, pageSize, pageCount);
List<Control> cs = new List<Control>();
//head links
if (startIndex != 0)
{
//<<
HyperLink first = new HyperLink();
first.NavigateUrl = url + "0";
first.Text = "<<";
cs.Add(first);
//...
HyperLink dotted = new HyperLink();
dotted.Text = "...";
dotted.NavigateUrl = url + (pageIndex - pageSize).ToString();
cs.Add(dotted);
}
//middle links
for (int i = startIndex; i <= endIndex; i++)
{
HyperLink lk = new HyperLink();
lk.Text = (i + 1).ToString();
lk.NavigateUrl = url + i.ToString();
if (i == pageIndex) lk.CssClass = "selected";
cs.Add(lk);
}
//tail links
if (endIndex != 0)
{
//...
HyperLink dotted = new HyperLink();
dotted.Text = "...";
dotted.NavigateUrl = url + (pageIndex + pageSize).ToString();
cs.Add(dotted);
//>>
HyperLink end = new HyperLink();
end.NavigateUrl = url + (pageCount - 1).ToString();
end.Text = ">>";
cs.Add(end);
}
foreach (Control c in cs)
{
Controls.Add(c);
}
}