Loading

简单的分页控件(原创)

前台有个容器用来存放创建的HtmlAnchor 
生成的样式:首页 上一页 12345 下一页 末页
   /// <summary>
///
/// </summary>
/// <param name="page">index first page is 0</param>
/// <returns></returns>
HtmlAnchor GetPageLink(int page)
{
HtmlAnchor a = new HtmlAnchor();
if (page < 0 || page > ItemCount / PageSize) return null;
//a.HRef = string.Format(Request.Path + (Request .QueryString .Count >0?string.Empty : "?")+"PageIndex={0}", page.ToString());
a.HRef =ToUriWithUserDomain(string.Format ("/HealthPostList/{0}.aspx",page.ToString ()));
a.InnerText = (page + 1).ToString();
return a;
}

void PageLinks()
{
Dictionary<int, int[]> pageIDs = new Dictionary<int, int[]>();
int pageCount = idList.Length / PageSize;
for (int i = 0, c = pageCount; i <= c; i++)
{
var item = idList.Skip(i * PageSize).Take(PageSize).ToArray ();
if (item.Length <= 0) continue;
pageIDs.Add(i,item);
}

idList = pageIDs[PageIndex];

//first page
HtmlAnchor firstPage =GetPageLink (0);
firstPage .InnerText ="首页";
PageLinkList .Controls .Add (firstPage);
HtmlAnchor prePage = GetPageLink((PageIndex - 1) < 0 ? 0 : (PageIndex - 1));
prePage.InnerText = "上一页";
PageLinkList.Controls.Add(prePage);

int range = 2;
int wholeRange = range * 2 + 1;
if (wholeRange > pageCount)
{
wholeRange = pageCount;
range = wholeRange / 2;
}
List<int> showPageIndex = new List<int>();
showPageIndex.Add(PageIndex);

while (showPageIndex.Count() < wholeRange)
{
showPageIndex.Add(showPageIndex.Max() + 1);
showPageIndex.Add(showPageIndex.Min() - 1);
if (PageIndex < range)
showPageIndex.Add(showPageIndex.Max() + 1);
if (PageIndex >= pageCount)
showPageIndex.Add(showPageIndex.Min() - 1);

showPageIndex = showPageIndex.OrderBy(item => item).ToList ();

showPageIndex = showPageIndex.Where(item => item >=0 && item < pageCount).ToList();

}

//showPageIndex.OrderBy(item => item);

foreach (int i in showPageIndex)
{
if(i==PageIndex )
{
HtmlGenericControl font = new HtmlGenericControl("font");
font.Attributes.Add("style", "font-size:24px");
font.Controls.Add(GetPageLink(PageIndex));
PageLinkList.Controls.Add(font);
}
else
PageLinkList.Controls.Add(GetPageLink(i));
}

HtmlAnchor nextPage = GetPageLink((PageIndex + 1) > (pageCount - 1) ? PageIndex : (PageIndex + 1));
nextPage.InnerText = "下一页";
PageLinkList.Controls.Add(nextPage);

HtmlAnchor endPage = GetPageLink((pageCount - 1)<0?0:(pageCount -1));
endPage.InnerText = "末页";
PageLinkList.Controls.Add(endPage);
}
版本二
   public partial class List : BaiChang .HeaBlog .Framework .HttpHandler .UserPage
    {
        
private int PageIndex = 0;
        
private int PageSize = 2;
        
private int ItemCount =10;
        
private int[] idList;
        Dictionary
<intint[]> pageIDs = new Dictionary<intint[]>();
        
protected void Page_Load(object sender, EventArgs e)
        {
            idList 
= EA.Posts.GetAllPostIDByUserID(base.BlogUser.UserID).Reverse().ToArray();
            ItemCount 
= idList.Length;
            PageIndex 
= -1;
            
if (!IsPostBack)
            {
                
if (Request.QueryString["PageIndex"!= null 
                    
&& 
                    
int.TryParse(Request.QueryString["PageIndex"].ToString (), out PageIndex)) { }
                
if (PageIndex < 0)
                    PageIndex 
= 0;
                PageLinks();
            }
            Array.ForEach
<int>(pageIDs[PageIndex],
            it 
=>
            {
                System.Web.UI.Control c 
= Utils.LoadShowHealBlogControl(it);
                Content.Controls.Add(c);
            });
           
        }

        
void PageLinks()
        {
            
int pageCount = idList.Length / PageSize;
            
for (int i = 0, c = pageCount; i <= c; i++)
            {
                var item 
= idList.Skip(i * PageSize).Take(PageSize).ToArray();
                
if (item.Length <= 0continue;
                pageIDs.Add(i, item);
            }

            idList 
= pageIDs[PageIndex];

            
//first page
            HtmlAnchor firstPage = GetPageLink(0true);
            firstPage.InnerText 
= "首页";
            PageLinkList.Controls.Add(firstPage);

            HtmlAnchor prePage 
= GetPageLink((PageIndex - 1< 0 ? 0 : (PageIndex - 1), true);
            
if (prePage != null)
            {
                prePage.InnerText 
= "上一页";
                PageLinkList.Controls.Add(prePage);
            }

            
int range = 2;
            
int wholeRange = range * 2 + 1;
            
if (wholeRange > pageCount)
            {
                wholeRange 
= pageCount;
                range 
= wholeRange / 2;
            }
            List
<int> showPageIndex = new List<int>();
            showPageIndex.Add(PageIndex);

            
while (showPageIndex.Count() < wholeRange)
            {
                showPageIndex.Add(showPageIndex.Max() 
+ 1);
                showPageIndex.Add(showPageIndex.Min() 
- 1);
                
if (PageIndex < range)
                    showPageIndex.Add(showPageIndex.Max() 
+ 1);
                
if (PageIndex >= pageCount)
                    showPageIndex.Add(showPageIndex.Min() 
- 1);

                showPageIndex 
= showPageIndex.OrderBy(item => item).ToList();

                showPageIndex 
= showPageIndex.Where(item => item >= 0 && item < pageCount).ToList();

            }

            
foreach (int i in showPageIndex)
            {
                
if (i == PageIndex)
                {
                    HtmlGenericControl font 
= new HtmlGenericControl("font");
                    font.Attributes.Add(
"style""font-size:24px");
                    System.Web.UI.Control c 
= GetPageLink(PageIndex, true);
                    
if (c != null)
                        font.Controls.Add(c);
                    PageLinkList.Controls.Add(font);
                }
                
else
                {
                    System.Web.UI.Control c 
= GetPageLink(i, true);
                    
if (c != null)
                        PageLinkList.Controls.Add(c);
                }
            }

            HtmlAnchor nextPage 
= GetPageLink((PageIndex + 1> (pageCount - 1? PageIndex : (PageIndex + 1), false);
            nextPage.InnerText 
= "下一页";
            PageLinkList.Controls.Add(nextPage);

            HtmlAnchor endPage 
= GetPageLink((pageCount - 1< 0 ? 0 : (pageCount - 1), false);
            endPage.InnerText 
= "末页";
            PageLinkList.Controls.Add(endPage);

            TextBox tb 
= new TextBox();
            tb.ID 
= "txtPageIndex";
            tb.MaxLength 
= 3;
            PageLinkList.Controls.Add(tb);

            Button btn 
= new Button();
            btn.ID 
= "btnTo";
            btn.Text 
= "GO";
            btn.Click 
+= new EventHandler(btn_Click);
            PageLinkList.Controls.Add(btn);
        }

        HtmlAnchor GetPageLink(
int page, bool isCheckIgnore)
        {

            HtmlAnchor a 
= new HtmlAnchor();
            
if (isCheckIgnore &&( page < 0 || page > ItemCount / PageSize)) return null;
            a.HRef 
= string.Format("{0}{1}/HealBlog/List/?PageIndex={2}", Utils.WebRoot, BlogUserDomain.Domain, page);
            
//a.HRef = string.Format(Request.Path + (Request .QueryString .Count >0?string.Empty: "?")+"PageIndex={0}", page.ToString());
            a.InnerText = (page + 1).ToString();
            
return a;
        }
        
void btn_Click(object sender, EventArgs e)
        {
            TextBox tb 
= (TextBox)Page.FindControl("btnTo");
            
if (tb != null)
            {
                
int.TryParse(tb.Text, out PageIndex);
            }
           
//在这里写跳转
        }

    }

posted @ 2009-10-27 16:31  today4king  阅读(430)  评论(1编辑  收藏  举报