數據綁定控件 簡單分頁

前臺頁面

           <asp:Repeater ID="rptNewsList" runat="server">
           <HeaderTemplate><ul></HeaderTemplate>
           <ItemTemplate>
           <li><a href='<%#"article.aspx?newsID="+Eval("id") %>' ><span class="left"><%#SubStr(Eval("title").ToString(),20) %></span></a><span class="right">[<%#((DateTime)Eval("timef")).ToShortDateString() %>]</span></li>
           </ItemTemplate>
           <FooterTemplate></ul></FooterTemplate>
           </asp:Repeater>


         <%--這是藺宜忠分頁--%>
    <div id="pageControl"  align="right">
    <asp:HiddenField ID="hfPageIndex" runat="server" Value="1" /><asp:HiddenField ID="hfPageCount" runat="server" />
    <table><tr>       
        <td valign="middle">
            <asp:Label ID="lblPageShow" runat="server"  Width="110px" ></asp:Label></td>
        <td valign="middle">&nbsp;&nbsp;
            <asp:LinkButton ID="lbFirst" runat="server"  OnClick="lbFirst_Click" >首 頁</asp:LinkButton></td>
        <td valign="middle"><asp:LinkButton ID="lbAbove" runat="server"  OnClick="lbAbove_Click" >上一頁</asp:LinkButton></td>
        <td valign="middle"><asp:LinkButton ID="lbNext" runat="server"  OnClick="lbNext_Click" >下一頁</asp:LinkButton></td>
        <td valign="middle"><asp:LinkButton ID="lbLast" runat="server"  OnClick="lbLast_Click" >尾 頁</asp:LinkButton></td>
        <td valign="middle"><asp:TextBox ID="txtFindPage" runat="server"  Width="30px" ></asp:TextBox></td>
        <td valign="middle"><asp:LinkButton ID="lbFindPage" runat="server"  onclick="lbFindPage_Click" >GO</asp:LinkButton></td>
       </tr></table>
      </div>
       <%--這是藺宜忠分頁--%>

 

後臺代碼

 

    #region  藺宜忠分頁用的
    //藺宜忠分頁方法
    private void pageChangeMethod()
    {
        int curPage = Convert.ToInt16(hfPageIndex.Value);
        PagedDataSource pds = new PagedDataSource();

        DataTable dt=***所要顯示分頁的數據源***
        pds.DataSource = dt.DefaultView;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = curPage - 1;
        pds.PageSize = 14;
        if (curPage == 1)
        {
            this.lbFirst.Enabled = false;
            lbAbove.Enabled = false;
        }
        else
        {
            this.lbFirst.Enabled = true;
            lbAbove.Enabled = true;
        }
        if (curPage == pds.PageCount)
        {
            this.lbLast.Enabled = false;
            lbNext.Enabled = false;

        }
        else
        {
            this.lbLast.Enabled = true;
            lbNext.Enabled = true;
        }
        rptNewsList.DataSource = pds;
        rptNewsList.DataBind();
        lblPageShow.Text = "當前頁:" + hfPageIndex.Value + "/" + pds.PageCount;
        txtFindPage.Text = hfPageIndex.Value;
        hfPageCount.Value = pds.PageCount.ToString();
    }
    #region 首頁 尾頁 下一頁...
    //首頁
    protected void lbFirst_Click(object sender, EventArgs e)
    {
        hfPageIndex.Value = "1";
        pageChangeMethod();
    }
    //上一頁
    protected void lbAbove_Click(object sender, EventArgs e)
    {
        hfPageIndex.Value = (Convert.ToInt16(hfPageIndex.Value) - 1).ToString();
        pageChangeMethod();
    }
    //下一頁
    protected void lbNext_Click(object sender, EventArgs e)
    {
        hfPageIndex.Value = (Convert.ToInt16(hfPageIndex.Value) + 1).ToString();
        pageChangeMethod();
    }
    //尾頁
    protected void lbLast_Click(object sender, EventArgs e)
    {
        hfPageIndex.Value = hfPageCount.Value;
        pageChangeMethod();
    }
    //目標頁
    protected void lbFindPage_Click(object sender, EventArgs e)
    {
        int goPage;
        try
        {
            goPage = Convert.ToInt32(txtFindPage.Text.Trim());
            if (goPage > 0)
            {
                if (goPage > Convert.ToInt32(hfPageCount.Value))
                {
                    hfPageIndex.Value = hfPageCount.Value;
                    pageChangeMethod();
                }
                else
                {
                    hfPageIndex.Value = goPage.ToString();
                    pageChangeMethod();
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('請輸入正確的頁碼');</script>");
            }
        }
        catch (Exception)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('請輸入正確的頁碼');</script>");
        }
    }
    #endregion
    #endregion

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章