datalist分页(二)

来源:互联网 发布:ubuntu怎么查看显卡 编辑:程序博客网 时间:2024/05/21 22:29

 自己也依照petshop4中的customlist凑合写了个customList

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections.Specialized;


/// <summary>
/// CustomList 的摘要说明
/// </summary>
namespace BGGCount.Web
{
    
public class CustomList:DataList
    {
        
//将数据及下面的页脚作为表格的两行来处理
        protected const string HTML1="<table cellpadding='0' cellspace='0'><tr><td colspan='2'>";
        
protected const string HTML2="</td><tr><td >";
        
protected const string HTML3="</td><td>";
        
protected const string HTML4="</td></tr></table>";

        
private string KEY_PAGE="page";
        
//替换page参数来达到换页的目的
        private static readonly Regex RX=new Regex(@"^[?&]page=d+$",RegexOptions.Compiled);
        
//左右页脚(显示页码)
        private  string strLeft="共有留言<font color='blue'><b>{0}</b></font>条 共有<font color='blue'><b>{1}</b></font>页 当前为第<font color='red'><b>{2}</b></font>页";
        
private  string strRight="";
        
private string HTMLURL = "<span style="width:5px;">&nbsp;&nbsp;</span><a href='?page={0}' title='{2}'>{1}</a>";
        
private string HTMLNOURL = "<span style='width:20px;color:#{0}' title='{2}'>&nbsp;&nbsp;{1}</span>";
        
//左右页脚是否可见
        private bool blnLeftVisible=true;
        
private bool blnRightVisible=true;
        
//无数据的情况
        protected string emptyText=String.Empty;
        
//数据源
        private IList dataSource;

        
//每页显示的条数,当前页数及总的数目,导航显示的页数
        private int pageSize=10;
        
private int currentPageIndex;
        
private int itemCount;
        
private int pageShow = 10;
        
        
override public object DataSource
        {
            
//This try catch block is to avoid issues with the VS.NET designer
            
//The designer will try and bind a datasource which does not derive from ILIST
            set
            {
                
try
                {
                    dataSource
=(IList)value;
                    itemCount
=dataSource.Count;
                }
                
catch
                {
                    dataSource
=null;
                    itemCount
=0;
                }                
            }
        }
        
public int PageSize
        {
            
set{pageSize=value;}
            
get{return pageSize;}
        }
        
public int CurrentPageIndex
        {
            
set{currentPageIndex=value;}
            
get{return currentPageIndex;}
        }
        
public int ItemCount
        {
            
set{itemCount=value;}
            
get{return itemCount;}
        }
        
public string EmptyText
        {
            
set{emptyText=value;}
        }
        
public int PageCount
        {
            
get{return (itemCount-1)/pageSize;}
        }
        
public bool BlnLeftVisible
        {
            
get { return blnLeftVisible; }
            
set { blnLeftVisible = value; }
        }
        
public bool BlnRightVisible
        {
            
get { return blnRightVisible; }
            
set { blnRightVisible = value; }
        }

        
//载入时触发页码改变事件
        protected override void  OnLoad(EventArgs e)
        {
            
//如果控件可见的话
              if(Visible)
             {
                 
string page=Context.Request[KEY_PAGE];
                 
int index=(page==null)?0:int.Parse(page);
                 SetPage(index);
             }
        }


        
public void SetPage(int index)
        {
             OnPageIndexChanged(
new DataGridPageChangedEventArgs(null,index));
        }
        
virtual protected  void OnPageIndexChanged(DataGridPageChangedEventArgs e)
        {
             
if(PageIndexChanged!=null)
            {
                PageIndexChanged(
this,e);
            }
        }
        
public event DataGridPageChangedEventHandler PageIndexChanged;        
        
        
//重载数据绑定,改变数据源中的数据
        protected override void  OnDataBinding(EventArgs e)
        {
            
int start=currentPageIndex*pageSize;
            
int end=Math.Min(start+pageSize,itemCount);
            IList pageSource
=new ArrayList();
            
for(int i=start;i<end;i++)
            {                
                pageSource.Add(((IList)dataSource)[i]);
            }
            
base.DataSource=pageSource;
            
base.OnDataBinding(e);
        }

        
//输出前输出javascript相关脚本
        protected override void OnPreRender(EventArgs e)
        {
            
if (itemCount > 0)
            {
                
//得到url中的参数
                string query = Context.Request.Url.Query.Replace("?""&");
                query 
= RX.Replace(query, string.Empty);

                
string js= ("<script type='text/javascript'>function changeClientPageIndex(pageindex){window.location.href='?page='+pageindex+'" + query + "';}</script>");
                ClientScriptManager cs 
= Page.ClientScript;
                Type cType 
= this.GetType();
                
if (!cs.IsClientScriptBlockRegistered(cType,"changeClientPageIndex"))
                {
                    cs.RegisterClientScriptBlock(cType, 
"changeClientPageIndex", js);
                }
            }
            
base.OnPreRender(e);
        }
        
//重载输出的内容,要增加输出页脚
        protected override void  Render(HtmlTextWriter writer)
        {
            
//检测是否有数据
            if(itemCount<=0)
            {
                writer.Write(emptyText);
                
return;
            }

           
//得到url中的参数
            string query = Context.Request.Url.Query.Replace("?""&");
            query 
= RX.Replace(query,string.Empty);

            writer.Write(HTML1);
             
base.Render(writer);
            writer.Write(HTML2);
            writer.Write(
string.Format(strLeft,ItemCount,PageCount+1,CurrentPageIndex+1));
            writer.Write(HTML3);
            
            
//writer.Write(strRight);
            
//输出首页跟上一页
            if (currentPageIndex > 0)
            {
                writer.Write(
string.Format(HTMLURL, "0" + query, "首页""转到第一页"));
                writer.Write(
string.Format(HTMLURL, (currentPageIndex-1+ query, "上一页""转到第" + (currentPageIndex) + ""));
            }
            
else
            {
                writer.Write(
string.Format(HTMLNOURL,"999999""首页""转到第一页"));
                writer.Write(
string.Format(HTMLNOURL,"999999""上一页""转到第" + (currentPageIndex) + ""));
            }
            
//如果不是第0-10页,输出相对前十页的链接地址
            if (currentPageIndex - currentPageIndex % 10 > 0)
            {
                writer.Write(
string.Format(HTMLURL, (currentPageIndex - currentPageIndex % 10-1+ query, "...""转到第" + (currentPageIndex - currentPageIndex % 10+ ""));
            }
            
//循环输出页面所在的十页
            for (int i = 0; i < 10 && i <= PageCount && (currentPageIndex - currentPageIndex % 10 + i <= PageCount); i++)
            {
                
//如果为当前页
                if(currentPageIndex - currentPageIndex % 10 + i==currentPageIndex)
                    writer.Write(
string.Format(HTMLNOURL, "FF0000", (currentPageIndex+1), "转到第" + (currentPageIndex+1+ ""));
                
else
                    writer.Write(
string.Format(HTMLURL, (currentPageIndex - currentPageIndex % 10 + i ) + query, (currentPageIndex - currentPageIndex % 10 + i + 1), "转到第" + (currentPageIndex - currentPageIndex % 10 + i + 1+ ""));
            }
            
//如果不是最后十页,输出相对后十页的链接地址
            if (currentPageIndex - currentPageIndex % 10 < PageCount - PageCount % 10)
            {
                writer.Write(
string.Format(HTMLURL, (currentPageIndex - currentPageIndex % 10+10+ query, "...""转到第" + (currentPageIndex - currentPageIndex % 10+11+ ""));
            }
            
//输出下一页跟尾页
            if (currentPageIndex!=PageCount)
            {
                writer.Write(
string.Format(HTMLURL, (currentPageIndex+1+ query, "下页""转到第" + (currentPageIndex+2+ ""));
                writer.Write(
string.Format(HTMLURL, (PageCount) + query, "尾页""转到第"+(PageCount+1)+""));
                
            }
            
else
            {
                writer.Write(
string.Format(HTMLNOURL, "999999""下页""转到第" + (currentPageIndex+2+ ""));
                writer.Write(
string.Format(HTMLNOURL, "999999""尾页""转到第" + (PageCount + 1+ ""));
                
            }

            
            
//输出跳转页            
            writer.Write("&nbsp;&nbsp;&nbsp;&nbsp;跳转至<select onchange='changeClientPageIndex(this.options[this.selectedIndex].value)'>");
            
for (int i = 0; i <= PageCount; i++)
            {
                
if (i == currentPageIndex)
                {
                    writer.Write(
"<option value='"+(i)+"' selected='selected'>第"+(i+1)+"页</option>");
                }
                
else
                {
                    writer.Write(
"<option value='" + (i) + "'>第" + (i + 1+ "页</option>");
                }
            }
            writer.Write(
"</select>");
            writer.Write(HTML4);            

        }
    }
}