ASP.NET Repeater_02_完整分页

本文内容

  • 分页基本1
  • 分页基本2
  • 分页按钮在 Repeater 控件内
  • 分页按钮在 Repeater 控件外

 

分页基本1

<%@ Page Language="C#" %>
 
<%@ Import Namespace="System.Data" %>
<!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>
 
    <script runat="server">
   1:  
   2:         private void BindData()
   3:         {
   4:             DataTable dt = ASPDotNetRepeater.DataSource.CreateEMPDataSource();
   5:             PagedDataSource pds = new PagedDataSource();
   6:             pds.DataSource = dt.DefaultView;
   7:             pds.AllowPaging = true;
   8:             pds.PageSize = 2;
   9:             int CurPage;
  10:             if (Request.QueryString["Page"] != null)
  11:             {
  12:                 CurPage = Convert.ToInt32(Request.QueryString["Page"]);
  13:             }
  14:             else
  15:             {
  16:                 CurPage = 1;
  17:             }
  18:             pds.CurrentPageIndex = CurPage - 1;
  19:             int Count = pds.PageCount;
  20:             CurrentPage.Text = "当前页:" + CurPage.ToString();
  21:             PageSize.Text = Count.ToString();
  22:             if (!pds.IsFirstPage)
  23:             {
  24:                 this.First.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
  25:                 this.Last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(Count - 1); ;
  26:                 Front.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
  27:             }
  28:             else
  29:             {
  30:                 this.First.Visible = false;
  31:                 this.Last.Visible = false;
  32:             }
  33:             if (!pds.IsLastPage)
  34:             {
  35:                 Next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
  36:             }
  37:             else
  38:             {
  39:                 this.First.Visible = false;
  40:                 this.Last.Visible = false;
  41:             }
  42:             Repeater1.DataSource = pds;
  43:             Repeater1.DataBind();
  44:         }
  45:         protected void Page_Load(object sender, EventArgs e)
  46:         {
  47:             if (!IsPostBack)
  48:             {
  49:                 if (!IsPostBack)
  50:                 {
  51:                     BindData();
  52:                     this.First.Visible = true;
  53:                     this.Last.Visible = true;
  54:                 }
  55:             }
  56:         }
  57:     
</script>
 
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td align="left">
                Repeater 控件
            </td>
        </tr>
        <tr>
            <td>
                <asp:Repeater ID="Repeater1" runat="server">
                    <HeaderTemplate>
                        <table>
                            <tr>
                                <td>
                                    自定义头模板
                                </td>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <a href='Rec.aspx?id=<%#Eval("EMPNO") %>' target="_blank">
                                    <%
   1: #Eval("ENAME")
%></a>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <AlternatingItemTemplate>
                        <tr>
                            <td>
                                <a href='Rec.aspx?id=<%#Eval("EMPNO") %>' target="_blank">
                                    <%
   1: #Eval("ENAME")
%></a>
                            </td>
                        </tr>
                    </AlternatingItemTemplate>
                    <FooterTemplate>
                        <tr>
                            <td>
                                自定义尾模板
                            </td>
                        </tr>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </td>
        </tr>
        <tr>
            <td>
                <asp:HyperLink ID="First" runat="server">首页</asp:HyperLink>
                <asp:HyperLink ID="Front" runat="server">上一页</asp:HyperLink>
                <asp:HyperLink ID="Next" runat="server">下一页</asp:HyperLink>
                <asp:HyperLink ID="Last" runat="server">尾页</asp:HyperLink>
            </td>
        </tr>
        <tr>
            <td>
                当前页为:<asp:Label ID="CurrentPage" runat="server" Text="Label"></asp:Label><br />
                <asp:Label ID="PageSize" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
说明

(1) Repeater 控件本身没有分页功能,但与其他类似的控件相比,它是比较底层的控件,尤其是它的分页功能;

(2) PagedDataSource 类的数据源必须是视图,如 DataTable.DefaultView,不能是 DataTable;

(3) 下一页、上一页等分页按钮不在 Repeater 里,而且分页的操作都在 BindData() 方法。

 

分页基本2

<%@ Page Language="C#" %>
 
<%@ Import Namespace="System.Data" %>
<!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>
 
    <script runat="server">
   1:  
   2:         public void BindData()
   3:         {
   4:             DataTable dt = ASPDotNetRepeater.DataSource.CreateEMPDataSource();
   5:             PagedDataSource pds = new PagedDataSource();
   6:             pds.DataSource = dt.DefaultView;
   7:             pds.AllowPaging = true;
   8:             pds.PageSize = 2;
   9:  
  10:             int curpage = Convert.ToInt32(CurrentPage.Text);
  11:             this.Next.Enabled = true;
  12:             this.Front.Enabled = true;
  13:             pds.CurrentPageIndex = curpage - 1;
  14:             if (curpage == 1)
  15:             {
  16:                 this.Front.Enabled = false;
  17:             }
  18:             if (curpage == pds.PageCount)
  19:             {
  20:                 this.Next.Enabled = false;
  21:             }
  22:             this.Repeater1.DataSource = pds;
  23:             this.Repeater1.DataBind();
  24:         }
  25:         protected void Page_Load(object sender, EventArgs e)
  26:         {
  27:             if (!IsPostBack)
  28:             {
  29:                 CurrentPage.Text = "1";
  30:                 BindData();
  31:             }
  32:         }
  33:         protected void Front_Click(object sender, EventArgs e)
  34:         {
  35:             this.CurrentPage.Text = Convert.ToString(Convert.ToInt32(CurrentPage.Text) - 1);
  36:             BindData();
  37:         }
  38:         protected void Next_Click(object sender, EventArgs e)
  39:         {
  40:             this.CurrentPage.Text = Convert.ToString(Convert.ToInt32(CurrentPage.Text) + 1);
  41:             BindData();
  42:         }
  43:     
</script>
 
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <td>
                        自定义头模板
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    员工编号:<%
   1: # Eval("EMPNO") 
%>
                </td>
                <td>
                    员工姓名:<%
   1: # Eval("ENAME") 
%>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            <tr>
                <td>
                    自定义脚模板
                </td>
            </tr>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    当前页:<asp:Label ID="CurrentPage" runat="server"></asp:Label>
    <br />
    <asp:Button ID="Front" runat="server" OnClick="Front_Click" Text="上一页" />
    <asp:Button ID="Next" runat="server" OnClick="Next_Click" Text="下一页" />
    </form>
</body>
</html>
说明

(1) 本例通过单击两个按钮,查看上一页和下一页的内容;

(2) Repeater 控件通过 PagedDataSource 实现分页功能。一些本身具有分页功能的控件,如 GridView,也都是使用了 PagedDataSource 类,所以 PagedDataSource 是比较底层的一个类;

 

分页按钮在 Repeater 控件内

<%@ Page Language="C#" %>
 
<%@ Import Namespace="System.Data" %>
<!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>
 
    <script runat="server">
   1:  
   2:         private PagedDataSource pds()
   3:         {
   4:             DataTable dt = ASPDotNetRepeater.DataSource.CreateEMPDataSource();
   5:             PagedDataSource pds = new PagedDataSource();
   6:             pds.DataSource = dt.DefaultView;
   7:             pds.AllowPaging = true;
   8:             pds.PageSize = 2;
   9:             pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
  10:             return pds;
  11:         }
  12:         protected void Page_Load(object sender, EventArgs e)
  13:         {
  14:             if (!IsPostBack)
  15:             {
  16:                 Repeater1.DataSource = pds();
  17:                 Repeater1.DataBind();
  18:             }
  19:         }
  20:         protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
  21:         {
  22:             if (e.Item.ItemType == ListItemType.Footer)
  23:             {
  24:                 DropDownList jump = (DropDownList)e.Item.FindControl("Jump");
  25:                 HyperLink first = (HyperLink)e.Item.FindControl("First");
  26:                 HyperLink front = (HyperLink)e.Item.FindControl("Front");
  27:                 HyperLink next = (HyperLink)e.Item.FindControl("Next");
  28:                 HyperLink last = (HyperLink)e.Item.FindControl("Last");
  29:                 Label currentPage = (Label)e.Item.FindControl("CurrentPage");
  30:                 Label pageSize = (Label)e.Item.FindControl("PageSize");
  31:  
  32:                 PagedDataSource pds = this.pds();
  33:                 pds.CurrentPageIndex = jump.SelectedIndex;
  34:                 int n = Convert.ToInt32(pds.PageCount);
  35:                 int i = Convert.ToInt32(pds.CurrentPageIndex);
  36:                 currentPage.Text = Convert.ToString(pds.CurrentPageIndex + 1);
  37:                 pageSize.Text = n.ToString();
  38:  
  39:                 if (!IsPostBack)
  40:                 {
  41:                     for (int j = 0; j < n; j++)
  42:                     {
  43:                         jump.Items.Add(Convert.ToString(j + 1));
  44:                     }
  45:                 }
  46:                 if (i <= 0)
  47:                 {
  48:                     first.Enabled = false;
  49:                     front.Enabled = false;
  50:                     last.Enabled = true;
  51:                     next.Enabled = true;
  52:                 }
  53:                 else
  54:                 {
  55:                     front.NavigateUrl = "?page=" + (i - 1);
  56:                 }
  57:                 if (i >= n - 1)
  58:                 {
  59:                     first.Enabled = true;
  60:                     last.Enabled = false;
  61:                     next.Enabled = false;
  62:                     front.Enabled = true;
  63:                 }
  64:                 else
  65:                 {
  66:                     next.NavigateUrl = "?page=" + (i + 1);
  67:                 }
  68:                 first.NavigateUrl = "?page=0";
  69:                 last.NavigateUrl = "?page=" + (n - 1);
  70:                 jump.SelectedIndex = Convert.ToInt32(pds.CurrentPageIndex);
  71:             }
  72:         }
  73:         protected void Jump_SelectedIndexChanged(object sender, EventArgs e)
  74:         {
  75:             string pageIndex = Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue) - 1));
  76:             Response.Redirect("PagingUsingRepeaterHandle.aspx?page=" + pageIndex);
  77:         }
  78:     
</script>
 
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <HeaderTemplate>
            <table width="500">
                <tr>
                    <td>
                        员工姓名
                    </td>
                    <td>
                        出生日期
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <a href='Rec.aspx?id=<%# Eval("EMPNO")%>'>
                        <%
   1: # Eval("ENAME") 
%></a>
                </td>
                <td>
                    <%
   1: # Eval("HIREDATE")
%>
                </td>
            </tr>
        </ItemTemplate>
        <SeparatorTemplate>
            <tr>
                <td colspan="2">
                    <hr style="border-top: 1pt;" />
                </td>
            </tr>
        </SeparatorTemplate>
        <FooterTemplate>
            <tr>
                <td colspan="2" style="font-size: 12pt">
                    <asp:HyperLink ID="First" runat="server" Text="首页"></asp:HyperLink>
                    <asp:HyperLink ID="Front" runat="server" Text="上一页"></asp:HyperLink>
                    <asp:HyperLink ID="Next" runat="server" Text="下一页"></asp:HyperLink>
                    <asp:HyperLink ID="Last" runat="server" Text="尾页"></asp:HyperLink>
                    <asp:Label ID="CurrentPage" runat="server" Text="Label"></asp:Label>页 共<asp:Label
                        ID="PageSize" runat="server" Text="Label"></asp:Label>页 跳至第<asp:DropDownList ID="Jump"
                            runat="server" AutoPostBack="true" OnSelectedIndexChanged="Jump_SelectedIndexChanged">
                        </asp:DropDownList>
                </td>
            </tr>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    </form>
</body>
</html>

 

分页按钮在 Repeater 控件外

<%@ Page Language="C#" %>
 
<%@ Import Namespace="System.Data" %>
<!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>
 
    <script runat="server">
   1:  
   2:         protected void Page_Load(object sender, EventArgs e)
   3:         {
   4:             if (!IsPostBack)
   5:             {
   6:                 this.CurrentPage.Text = "1";
   7:                 this.BindData();
   8:             }
   9:         }
  10:         public void BindData()
  11:         {
  12:             DataTable dt = ASPDotNetRepeater.DataSource.CreateEMPDataSource();
  13:             PagedDataSource pds = new PagedDataSource();
  14:             pds.DataSource = dt.DefaultView;
  15:             pds.AllowPaging = true;
  16:             pds.PageSize = 2;
  17:             pds.CurrentPageIndex = Convert.ToInt32(this.CurrentPage.Text) - 1;
  18:  
  19:             PageSize.Text = pds.PageCount.ToString();
  20:             CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
  21:             this.Front.Enabled = true;
  22:             this.First.Enabled = true;
  23:             this.Next.Enabled = true;
  24:             this.Last.Enabled = true;
  25:  
  26:  
  27:             if (!IsPostBack)
  28:             {
  29:                 for (int j = 0; j < pds.PageCount; j++)
  30:                 {
  31:                     this.Jump.Items.Add(Convert.ToString(j + 1));
  32:                 }
  33:             }
  34:  
  35:             if (pds.CurrentPageIndex < 1)
  36:             {
  37:                 this.Front.Enabled = false;
  38:                 this.First.Enabled = false;
  39:             }
  40:             if (pds.CurrentPageIndex == pds.PageCount - 1)
  41:             {
  42:                 this.Next.Enabled = false;
  43:                 this.Last.Enabled = false;
  44:             }
  45:             Repeater1.DataSource = pds;
  46:             Repeater1.DataBind();
  47:         }
  48:         protected void First_Click(object sender, EventArgs e)
  49:         {
  50:             this.CurrentPage.Text = "1";
  51:             this.BindData();
  52:         }
  53:         protected void Front_Click(object sender, EventArgs e)
  54:         {
  55:             this.CurrentPage.Text = Convert.ToString(Convert.ToInt32(CurrentPage.Text) - 1);
  56:             this.BindData();
  57:         }
  58:         protected void Next_Click(object sender, EventArgs e)
  59:         {
  60:             this.CurrentPage.Text = Convert.ToString(Convert.ToInt32(CurrentPage.Text) + 1);
  61:             this.BindData();
  62:         }
  63:         protected void Last_Click(object sender, EventArgs e)
  64:         {
  65:             this.CurrentPage.Text = this.PageSize.Text;
  66:             this.BindData();
  67:         }
  68:         protected void Jump_SelectedIndexChanged(object sender, EventArgs e)
  69:         {
  70:             string pageIndex = Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue) - 1));
  71:             this.CurrentPage.Text = pageIndex;
  72:             this.BindData();
  73:         }
  74:     
</script>
 
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <tr>
                    <%
   1: --前台也可以调用后台的自定义函数--
%>
                    <td>
                        <a href="">
                            <%
   1: # DataBinder.Eval(Container.DataItem,"[EMPNO]")
%></a>
                    </td>
                    <td>
                        <a href="">
                            <%
   1: # DataBinder.Eval(Container.DataItem,"[ENAME]")
%></a>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
    <table border="1">
        <tr>
            <td>
                <asp:LinkButton ID="First" runat="server" OnClick="First_Click">页首</asp:LinkButton>
                <asp:LinkButton ID="Front" runat="server" OnClick="Front_Click">上一页</asp:LinkButton>
                <asp:LinkButton ID="Next" runat="server" OnClick="Next_Click">下一页</asp:LinkButton>
                <asp:LinkButton ID="Last" runat="server" OnClick="Last_Click">页尾</asp:LinkButton>
<asp:Label ID="CurrentPage" runat="server" Text="Label"></asp:Label>页/共<asp:Label
                    ID="PageSize" runat="server" Text="Label"></asp:Label>页 跳至<asp:DropDownList ID="Jump"
                        runat="server" AutoPostBack="true" OnSelectedIndexChanged="Jump_SelectedIndexChanged">
                    </asp:DropDownList>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
说明

(1) 将分页按钮放在了 Repeater 的 FooterTemplate,所以要实现分页功能,就需要 Repeater 的 ItemDataBound 事件,该事件在数据绑定后,并在数据显示前出发;

(2) 必须用视图 DataTable.DefaultView 给 PagedDataSource.DataSource 赋值;

(3) 传递页码的方式是采用通过超链接向当前页面传参数。

 

下载 Demo

posted @ 2011-08-30 21:57  船长&CAP  阅读(298)  评论(0编辑  收藏  举报
免费流量统计软件