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

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

repeater分页自定义控件  

2010-06-12 15:12:58|  分类: .net技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
PageDataSetControl.cs
--------------------------------------------------------------------------------------------------------------------------------------
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.Text.RegularExpressions;
using System.Data;

namespace KlControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:PageDataSetControl runat=\"server\" EmptyText=\"No Items found.\" OnPageIndexChanged=\"PageChanged\" PageSize=\"6\"  NavigationNums=\"7\" ></{0}:PageDataSetControl>")]
    public class PageDataSetControl : Repeater
    {
         
        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 PagedDataSource 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 = (PagedDataSource)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);

            dataSource.CurrentPageIndex =  (CurrentPageIndex -1);
            dataSource.PageSize = pageSize;


            base.DataSource = dataSource;
            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);
        }

    }
}

测试类:
test.aspx
-----------------------------------------------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test1.aspx.cs" Inherits="test1" %>

<%@ Register assembly="KlControls" namespace="KlControls" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
   
    <cc1:PageDataSetControl ID="PageDataSetControl1" runat="server"
        EmptyText="No Items found." NavigationNums="7" OnPageIndexChanged="PageChanged"
        PageSize="5"  >
        <ItemTemplate>
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <td valign="top" width="150"> <%# DataBinder.Eval(Container.DataItem, "Name")%></td>
                     <td valign="top" width="100"> <%# DataBinder.Eval(Container.DataItem, "Descn")%></td> 
                </tr>              
            </table>           
    </ItemTemplate>
    </cc1:PageDataSetControl>
  
</body>
</html>

test.aspx.cs
------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PetShop.BLL;

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

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

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

        //bind data
        string categoryKey = Request.QueryString["categoryId"];
        if (String.IsNullOrEmpty(categoryKey))
            categoryKey = "BIRDS";

        //bind data
        Product product = new Product();
        System.Data.DataSet ds = product.GetProductsByCategoryToDs(categoryKey);

        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = ds.Tables[0].DefaultView;
        objPds.AllowPaging = true;

        PageDataSetControl1.DataSource = objPds;
        PageDataSetControl1.DataBind();
    

    }
}
  评论这张
 
阅读(876)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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