登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

分页自定以控件  

2010-06-11 14:44:27|  分类: .net技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text.RegularExpressions;

namespace KlControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:PageListControl runat=\"server\" EmptyText=\"No Items found.\" OnPageIndexChanged=\"PageChanged\" PageSize=\"6\"  NavigationNums=\"7\" RepeatColumns=\"2\"></{0}:PageListControl>")]
    public class PageListControl : DataList
    {

        protected const string HTML1 = "<table cellpadding=0 cellspacing=0> <tr><td colspan=1> ";
        protected const string HTML2 = "</td></tr><tr><td class=paging align=left>";
        //Render
        //protected const string HTML3 = "</td><td align=right class=paging>";
        protected const string HTML4 = "</td></tr></table>";
        private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
        private const string LINK_PREV = "<a href=?page={0}>&#060;&#060;&nbsp;&nbsp;</a>";
        private const string LINK_MORE = "<a href=?page={0}>&nbsp;&#062;&#062;</a>";

        private const string LINK_NoPage = "<a href=?page={0}>{1}&nbsp;&nbsp;</a>";
        private const string LINK_CurPage = "<a href=?page={0}><font color=red>{1}&nbsp;&nbsp;</font></a>";

        private const string KEY_PAGE = "page";
        private const string COMMA = "?";
        private const string AMP = "&";

        protected string emptyText;
        private IList dataSource;
        private int pageSize = 10;
        private int currentPageIndex;
        private int itemCount;
        private int navigationNums = 7; //导航条数字个数

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int NavigationNums
        {
            get { return this.navigationNums; }
            set { this.navigationNums = value; }

        }

        override public object DataSource
        {
            set
            {
                //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
                try
                {
                    dataSource = (IList)value;
                    ItemCount = dataSource.Count;
                }
                catch
                {
                    dataSource = null;
                    ItemCount = 0;
                }
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        protected int PageCount
        {
            get
            {
                int i = (ItemCount ) % pageSize;
                return (ItemCount) / pageSize + ((i > 0) ? 1 : 0);
            }
        }

        //
        virtual protected int ItemCount
        {
            get { return itemCount; }
            set { itemCount = value; }
        }

        //用户接口方法,运行时候参数变化
        virtual public int CurrentPageIndex
        {
            get { return currentPageIndex; }
            set { currentPageIndex = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string EmptyText
        {
            set { emptyText = value; }
        }

        public void SetPage(int index)
        {
            OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
        }

        override protected void OnLoad(EventArgs e)
        {

            if (Visible)
            {
                string page = Context.Request[KEY_PAGE];
                int index = (page != null) ? int.Parse(page) : 1;
                SetPage(index);
            }
        }
        override protected void Render(HtmlTextWriter writer)
        {

            //Check there is some data attached
            if (ItemCount == 0)
            {
                writer.Write(emptyText);
                return;
            }

            //Mask the query
            string query = Context.Request.Url.Query;
            if (!String.IsNullOrEmpty(query))
            {
                query = query.Replace(COMMA, AMP);
            }

            query = RX.Replace(query, string.Empty);


            // Write out the first part of the control, the table header
            writer.Write(HTML1);


            // Call the inherited method
            base.Render(writer);

            // Write out a table row closure
             writer.Write(HTML2);

            // Call the inherited method
            //base.Render(writer);

            //Determin whether next and previous buttons are required
            //Previous button?


            if (currentPageIndex > 1)
                writer.Write(string.Format(LINK_PREV, (currentPageIndex - 1) + query));


            if (currentPageIndex > PageCount)
            {
                currentPageIndex = PageCount;

            }

            //右边距离中间点距离
            int d =  NavigationNums/2 - (PageCount - currentPageIndex) -1 ;
           
            int initPage = currentPageIndex - NavigationNums / 2;
           
            //不为导航条数据一半
            if (d > 0)
                initPage = initPage - d;
            //
            if (initPage < 1)
                initPage = 1;

            //数字导航
            for (int i = initPage, j = 0; i <= PageCount && j < NavigationNums; i++, j++)
            {
                if (currentPageIndex == i)
                {
                    writer.Write(string.Format(LINK_CurPage, (i) + query, i));
                }
                else
                {
                    writer.Write(string.Format(LINK_NoPage, (i) + query, i));
                }

            }

            //Close the table data tag
            //writer.Write(HTML3);

            //Next button?
            if (currentPageIndex < PageCount)
                writer.Write(string.Format(LINK_MORE, (currentPageIndex + 1) + query));

            //Close the table
            writer.Write(HTML4);
            
        }

        override protected void OnDataBinding(EventArgs e)
        {

            //Work out which items we want to render to the page
            if (CurrentPageIndex > PageCount)
            {
                CurrentPageIndex = PageCount - 1;

            }
            //首页从1开始
            if (CurrentPageIndex < 1)
            {
                CurrentPageIndex = 1;
            }
            int start = (CurrentPageIndex-1) * pageSize;
            int size = Math.Min(pageSize, ItemCount - start);

            IList page = new ArrayList();

            //Add the relevant items from the datasource
            for (int i = 0; i < size; i++)
                page.Add(dataSource[start + i]);

            //set the base objects datasource
            base.DataSource = page;
            base.OnDataBinding(e);

        }
        //定义事件
        //public delegate void DataGridPageChangedEventHandle 在WebControl中声明
        public event DataGridPageChangedEventHandler PageIndexChanged;

        //控件上显示事件On+时间名
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e)
        {
            if (PageIndexChanged != null)
                PageIndexChanged(this, e);
        }

    }
}
测试类:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<%@ Register Assembly="KlControls" Namespace="KlControls" TagPrefix="cc1" %>
 <cc1:PageListControl ID="PageListControl1" runat="server" EmptyText="No Items found."
            NavigationNums="20" OnPageIndexChanged="PageChanged" PageSize="25">
    <ItemTemplate>
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <td valign="top" width="150"> <%# Eval("name") %></td>
                     <td valign="top" width="100"> <%# Eval("price") %></td> 
                </tr>              
            </table>           
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
</cc1:PageListControl>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PetShop.Model;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        //reset index
        PageListControl1.CurrentPageIndex = e.NewPageIndex;

        //get category id
        //string categoryKey = Request.QueryString["categoryId"];

        //bind data
        PageListControl1.DataSource = getList();
        PageListControl1.DataBind();

    }

    public IList<ProductTest> getList()
    {

        IList<ProductTest> list = new List<ProductTest>();
        for(int i = 0;i<1000;i++)
        {
            list.Add(new ProductTest("product" + i, i+""));
        }
       
        return list;
    }
}
[Serializable]
public class ProductTest
{
    private string name;
    private string price;

    public ProductTest(string n, string p)
    {
        name = n;
        price = p;
    }
    public string Name
    {
        get { return name; }
    }

    public string Price
    {
        get { return price; }
    }
}
  评论这张
 
阅读(688)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018