asp.net Repeater使用例子,包括分页

<style type="text/css">
    .tab{border-collapse:collapse; margin:0 auto;}
    .tab th{ border:#000 solid 1px; line-height:24px;}
    .tab td{border:#000 solid 1px; line-height:18px;}
    .tab td.no{color:#f00;}
    </style>

 

<table class="tab">
        <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" >
        <HeaderTemplate>
        <tbody>
            <tr>
                <th>555</th>
                <th>666</th>
                <th>777</th>
            </tr>
         </tbody>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%# DataBinder.Eval(Container.DataItem,"SendCentreID") %>
                </td>
                <td><%# DataBinder.Eval(Container.DataItem,"Mo") %>
                </td>
                <td><%# DataBinder.Eval(Container.DataItem,"Stat") %>
                </td>
                <td>
                    <asp:Button ID="Button3" runat="server" UseSubmitBehavior="true" Text="Button" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ID") %>' CommandName="ButtonOnClick" />
                </td>
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr>
                <td class="no"><%# DataBinder.Eval(Container.DataItem,"SendCentreID") %>
                </td>
                <td class="no"><%# DataBinder.Eval(Container.DataItem,"Mo") %>
                </td>
                <td class="no"><%# DataBinder.Eval(Container.DataItem,"Stat") %>
                </td>
                <td>
                    <asp:Button ID="Button3" runat="server" Text="Button" UseSubmitBehavior="true" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ID") %>' CommandName="ButtonOnClick" />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
            <tr>
                <td colspan="4">
                    <asp:LinkButton ID="FirstPage" runat="server" OnClick="FirstPage_OnClick">首页</asp:LinkButton>
                
                    <asp:LinkButton ID="UpPage" runat="server" OnClick="UpPage_OnClick">上一页</asp:LinkButton>
                    
                    第<asp:Label ID="ThisPage" runat="server" Text="1"></asp:Label>页 共<asp:Label ID="AllCountPage" runat="server" Text="1"></asp:Label>页
                    
                    <asp:LinkButton ID="NextPage" runat="server" OnClick="NextPage_OnClick">下一页</asp:LinkButton>
                
                    <asp:LinkButton ID="LastPage" runat="server" OnClick="LastPage_OnClick">末页</asp:LinkButton>
                    
                    <asp:TextBox ID="CountPage" runat="server"></asp:TextBox>
                    <asp:LinkButton ID="SetCountPage" runat="server" OnClick="SetCountPage_OnClick">转</asp:LinkButton>
                </td>
            </tr>
        </FooterTemplate>
        </asp:Repeater>
        </table>

后台代码

public struct PageState
    {

        /// <summary>
        /// 当前页
        /// </summary>
        public int ThisPage;
        /// <summary>
        /// 要显示的页数
        /// </summary>
        public int PageSize;
        /// <summary>
        /// 总页数
        /// </summary>
        public int AllPageCount;
        /// <summary>
        /// 记录数
        /// </summary>
        public int Counts;
    }
    protected PageState pagestate;
    protected String AdoConn = ConfigurationSettings.AppSettings["XueXunTongConn"];
    protected void Page_Load(object sender, EventArgs e)
    {
        pagestate.PageSize = 16;
        pagestate.ThisPage = 1;
        if (!Page.IsPostBack)
        {
            GetData("");
        }
    }
    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        
    }

    /// <summary>
    /// 设置当然页数
    /// </summary>
    /// <param name="Count"></param>
    private void SetCount()
    {
        if (pagestate.ThisPage > pagestate.AllPageCount)
        {
            pagestate.ThisPage = pagestate.AllPageCount;
        }
        ((Label)Repeater1.Controls[this.Repeater1.Controls.Count - 1].FindControl("ThisPage")).Text = pagestate.ThisPage.ToString();
        TextBox TB = (TextBox)Repeater1.Controls[this.Repeater1.Controls.Count - 1].FindControl("CountPage");
        TB.Text = pagestate.ThisPage.ToString();
        ((Label)Repeater1.Controls[this.Repeater1.Controls.Count - 1].FindControl("AllCountPage")).Text = pagestate.AllPageCount.ToString();
    }

    protected void GetData(String Where)
    {
        DataSet DS = SQLHelp.PageView("SendCentreMo", "*", pagestate.PageSize, pagestate.ThisPage, ref pagestate.AllPageCount, ref pagestate.Counts, "id", true, Where, "id", false);
       Repeater1.DataSource = DS;
       Repeater1.DataBind();
       SetCount();
    }
    //转
    protected void SetCountPage_OnClick(object sender, EventArgs e)
    {
        TextBox TB = (TextBox)Repeater1.Controls[this.Repeater1.Controls.Count - 1].FindControl("CountPage");
        if (TB.Text == "")
        {
            return;
        }
        try
        {
            pagestate.ThisPage = Int32.Parse(TB.Text);
            if (pagestate.ThisPage <= 0)
            {
                pagestate.ThisPage = 1;
            }
        }
        catch
        {
            pagestate.ThisPage = 1;
        }
        GetData("");
    }
    //首页
    protected void FirstPage_OnClick(object sender, EventArgs e)
    {
        pagestate.ThisPage = 1;
        GetData("");
    }
    //末页
    protected void LastPage_OnClick(object sender, EventArgs e)
    {
        try
        {
            pagestate.ThisPage = Int32.Parse(((Label)Repeater1.Controls[this.Repeater1.Controls.Count - 1].FindControl("AllCountPage")).Text);
        }
        catch
        {
            pagestate.ThisPage = 1;
        }
        GetData("");
    }
    //上一页
    protected void UpPage_OnClick(object sender, EventArgs e)
    {
        try
        {
            pagestate.ThisPage = Int32.Parse(((Label)Repeater1.Controls[this.Repeater1.Controls.Count - 1].FindControl("ThisPage")).Text);
            pagestate.ThisPage--;
        }
        catch
        {
            pagestate.ThisPage = 1;
        }

        if (pagestate.ThisPage <= 0)
        {
            pagestate.ThisPage = 1;
        }
        GetData("");
    }
    //下一页
    protected void NextPage_OnClick(object sender, EventArgs e)
    {
        try
        {
            pagestate.ThisPage = Int32.Parse(((Label)Repeater1.Controls[this.Repeater1.Controls.Count - 1].FindControl("ThisPage")).Text);
            pagestate.ThisPage++;
        }
        catch
        {
            pagestate.ThisPage = 1;
        }
        GetData("");
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "ButtonOnClick":
                int i = Convert.ToInt32(e.CommandArgument);
                break;
        }
    }

原文地址:https://www.cnblogs.com/mili3/p/3260017.html