asp.net自定义分页

2007年06月26日 星期二 下午 04:11

妈哟,晕死了,刚快要把文章写完的时候,不小心按了下BackSpace键,,。全完了!!!!!

只好重写,,,,

费话不多说了,这里讲的自定义分页是指自定义一个控件,从数据库中读取出指定的对象,

不像GridView一样是把全部数据读取到内存中来,这样就更快一些,,,好看些,,

在这之前,先看两个方法,一个是返回对象的个数,另一个是返回指定的对象组,

我这里用的是Nhibernate,所以如果没用Nhibernate的话,就自己另想方法了,

public int Count()
            {
                try
                {
                    CreateSession();
                    int count = session.CreateCriteria(typeof(T)).List().Count;
                    session.Close();
                    return count;
                }
                catch (Exception erroy)
                {
                    throw erroy;
                }
            }

public IList GetPage(int first,int count)
            {
                try
                {
                    CreateSession();
                    ICriteria criteria = session.CreateCriteria(typeof(T));
                    criteria.SetFirstResult(first);
                    criteria.SetMaxResults(count);
                    IList list = criteria.List();
                    return list;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

添加一个自定义控件:<a id="pages" runat="server"></a>

protected void Page_Load(object sender, EventArgs e)
     {
     }

     private int pageCount;
     /// <summary>
     /// 0--N
     /// </summary>
     public int PageCount
     {
         get
         {
             return pageCount;
         }
         set
         {
             pageCount = value;
             if (this.UserPage != null)
             {
                 SetPages();
             }
         }
     }

     private Page userPage;

     public Page UserPage
     {
         get
         {
             return userPage;
         }
         set
         {
             userPage = value;
             if (this.PageCount != 0)
             {
                 SetPages();
             }
         }
     }

     private void SetPages()
     {
         int pageIndex = Convert.ToInt32(Request["pageIndex"]);
         string strUrl = GetUrl();
         pages.InnerHtml += "<a href='" + strUrl + "?pageIndex=1' style='color:blue'>[首页]</a>";
         int up = pageIndex - 1;
         if (up >= 1)
         {
             pages.InnerHtml += "<a href='" + strUrl + "?pageIndex=" + up + "' style='color:blue'>[上一页]</a>";
         }
         for (int i = 0; i < pageCount; i++)
         {
             int p = i + 1;
             if (pageIndex == p)
             {
                 pages.InnerHtml += "<a href='" + strUrl + "?pageIndex=" + p + "' style='color:red'>" + p + "</a>";
             }
             else
             {
                 if (pageIndex == 0 && p == 1)
                 {
                     pages.InnerHtml += "<a href='" + strUrl + "?pageIndex=" + p + "' style='color:red'>1</a>";
                 }
                 else
                 {
                     pages.InnerHtml += "<a href='" + strUrl + "?pageIndex=" + p + "' style='color:blue'>[" + p + "]</a>";
                 }
             }
         }
         int down = pageIndex + 1;
         if (down <= pageCount)
         {
             if (down == 1)
             {
                 down++;
             }
             pages.InnerHtml += "<a href='" + strUrl + "?pageIndex=" + down + "' style='color:blue'>[下一页]</a>";
         }
         pages.InnerHtml += "<a href='" + strUrl + "?pageIndex=" + this.pageCount + "' style='color:blue'>[末页]</a>";
     }

     private string GetUrl()
     {
         string pageName = UserPage.AppRelativeVirtualPath;
         pageName = pageName.Remove(0, 1);
         string strServer = "http://" + Request.ServerVariables["SERVER_NAME"].ToString();
         string strPort = ":" + Convert.ToString(Request.ServerVariables["SERVER_PORT"]);
         string strRoot = Request.ApplicationPath;
         if (strPort.Trim() == ":80")
         {
             strPort = "";
         }
         return strServer + strPort + strRoot + pageName;
     }

添加一个页面,添加一个GridView ,PageSize设为5,把对象的属性全绑定到GridView的数据绑定列上。

使用自定义控件: <UserControls:TPage ID="test" runat="server" />

写方法: private void LoadData()
     {
         test.UserPage = this;
         test.PageCount = Convert.ToInt32((new UsersManage().Count() / grid_users.PageSize) + 1);
        
         int pager = Convert.ToInt32(Request["pageIndex"]) - 1;
         if (pager < 0)
         {
             pager = 0;
         }
         grid_users.DataSource = new UsersManage().GetPage(pager * grid_users.PageSize, 5);
         grid_users.DataBind();
     }

在Page_Load的时候调用它,

哎,,才发现,这个写文章原来是可以放图片进来的,我真白吃,

效果图如下:

还有IE上的图,也一起放上来算了,

OK啦,,这样的分页,的确是要比GridView自带的分页好多啦,,,不信,,你试试。哈哈。
肯定还有待完善一些东西,比如百度上,前点到12页之后的页码,前面的就不会显示出来了,
呵呵,,以后再做吧,
posted @ 2008-02-01 10:37  point.deng  阅读(581)  评论(0编辑  收藏  举报