自己开发的一款基于PagedDataSource的datalist repeater控件,只需要在源程序中添加三行代码,即可实现通用分页

第一步:

在使用到自定义分页的后台代码中添加以下三行

 protected void Page_Load(object sender, EventArgs e)
      { 
        UIShareMethods.InitPds(DbHelper.GetDataSet("select * from a1User").DataSet, 10, DataList1);
          if (!Page.IsPostBack)
          {
              DataList1.DataSource = UIShareMethods.Pds;
              DataList1.DataBind();
          }
     }

 

第二步:

在自定义公共类UIShareMethods中添加以下代码:

  public static PagedDataSource Pds;
    public static DataList datlst;

    /// <summary>
    /// 初始化控件PagedDataSource
    /// </summary>
    /// <param name="ds">所在页面需要翻页的datalist对应dataset</param>
    /// <param name="pagesize">所在页面包含的记录数目</param>
    /// <param name="dl">所在页面需要翻页的datalist</param>

    public static void InitPds(DataSet ds,int pagesize,DataList dl)
    {
       
        Pds = new PagedDataSource();
        Pds.DataSource = ds.Tables[0].DefaultView;
        Pds.AllowPaging = true;
        Pds.PageSize = pagesize;
        datlst = dl;
    }

 

第三步:

创建自定义控件:dalistPager.ascx

代码如下:

1)dalistPager.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="dalistPager.ascx.cs" Inherits="dalistPager" %>
<table style=" width:50%; font-size:small; text-decoration:none">
    <tr>
        <td nowrap="nowrap">
            <asp:LinkButton ID="lbFirst" runat="server" onclick="lbFirst_Click">第一页</asp:LinkButton>
        </td>
        <td nowrap="nowrap">
            <asp:LinkButton ID="lbPrev" runat="server" onclick="lbPrev_Click">上一页</asp:LinkButton>
        </td>
        <td nowrap="nowrap">
            <asp:LinkButton ID="lbNext" runat="server" onclick="lbNext_Click">下一页</asp:LinkButton>
        </td>
        <td nowrap="nowrap">
            <asp:LinkButton ID="lbLast" runat="server" onclick="lbLast_Click">最后一页</asp:LinkButton>
        </td>
        <td nowrap="nowrap">
            第<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
            页/共<asp:Label ID="lblPageCount" runat="server"></asp:Label>
            页</td>
        <td nowrap="nowrap">
            <asp:TextBox ID="txtPageNum" runat="server" Width="27px"></asp:TextBox>
            <asp:LinkButton ID="lbGo" runat="server" onclick="lbGo_Click">Go</asp:LinkButton>
        </td>
    </tr>
</table>
<p>
     </p>

2)dalistPager.ascx.cs

public partial class dalistPager : System.Web.UI.UserControl
{
    static int currentpageindex=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        lblPageCount.Text = UIShareMethods.Pds.PageCount.ToString();
        lblCurrentPage.Text = (currentpageindex+1).ToString();
        btnState();
    }


    protected void lbFirst_Click(object sender, EventArgs e)
    {
        UIShareMethods.Pds.CurrentPageIndex = currentpageindex;
        if (UIShareMethods.Pds.CurrentPageIndex >= 0)
        {
            UIShareMethods.Pds.CurrentPageIndex = 0;
            currentpageindex = 0;
            //lblCurrentPage.Text = currentpageindex.ToString();
            UIShareMethods.datlst.DataSource = UIShareMethods.Pds;
            UIShareMethods.datlst.DataBind();          
        }
        else
        {
            return;
        }
        

    }

    
    protected void lbPrev_Click(object sender, EventArgs e)
    {
        UIShareMethods.Pds.CurrentPageIndex = currentpageindex;
        if (UIShareMethods.Pds.CurrentPageIndex > 0)
        {
            currentpageindex=--(UIShareMethods.Pds.CurrentPageIndex);
            //lblCurrentPage.Text = currentpageindex.ToString();
            UIShareMethods.datlst.DataSource = UIShareMethods.Pds;
            UIShareMethods.datlst.DataBind();
        }
        else
        {
            return;
        }
    }
    protected void lbNext_Click(object sender, EventArgs e)
    {
        UIShareMethods.Pds.CurrentPageIndex = currentpageindex;
        if (UIShareMethods.Pds.CurrentPageIndex < UIShareMethods.Pds.PageCount-1)
        {
           currentpageindex=++( UIShareMethods.Pds.CurrentPageIndex);
           //lblCurrentPage.Text = currentpageindex.ToString();
            UIShareMethods.datlst.DataSource = UIShareMethods.Pds;
            UIShareMethods.datlst.DataBind();      
        }
        else
        {
            return;
        }
    }
    protected void lbLast_Click(object sender, EventArgs e)
    {
        UIShareMethods.Pds.CurrentPageIndex = currentpageindex;
        if (UIShareMethods.Pds.CurrentPageIndex < UIShareMethods.Pds.PageCount - 1)
        {
            UIShareMethods.Pds.CurrentPageIndex = UIShareMethods.Pds.PageCount - 1;
            currentpageindex = UIShareMethods.Pds.PageCount - 1;
            //lblCurrentPage.Text = currentpageindex.ToString();
            UIShareMethods.datlst.DataSource = UIShareMethods.Pds;
            UIShareMethods.datlst.DataBind();          
        }
        else
        {
            return;
        }
    }
    protected void lbGo_Click(object sender, EventArgs e)
    {
        UIShareMethods.Pds.CurrentPageIndex = int.Parse(txtPageNum.Text)-1;
        currentpageindex = int.Parse(txtPageNum.Text)-1;
        //lblCurrentPage.Text = txtPageNum.Text;
        UIShareMethods.datlst.DataSource = UIShareMethods.Pds;
        UIShareMethods.datlst.DataBind();
    }

    protected void btnState()
    {
        if (currentpageindex==0)
        {
             lbFirst.Enabled = false;
            lbPrev.Enabled = false;
            lbNext.Enabled = true;
            lbLast.Enabled = true;
        }
        else if (currentpageindex==UIShareMethods.Pds.PageCount - 1)
        {
             lbFirst.Enabled = true;
            lbPrev.Enabled = true;
            lbNext.Enabled = false;
            lbLast.Enabled = false;
        }
        else
        {
            lbFirst.Enabled = true;
            lbPrev.Enabled = true;
            lbNext.Enabled = true;
            lbLast.Enabled = true;
        }
        
    }
}

 

 

第四步:

将自定义控件拖放到第一步中页面中的datalist的<footertemplate></footertemplate>中

 

 

 

 

你可能感兴趣的:(r,DataList分页,自定义通用控件)