2012年10月29日 星期一

DataList分頁

承辦說資料越來越多所以要分頁,但DataList不像GridView有現成的pager元件可以分頁(對岸似乎也有人可以把pager套在DataList上的但我不會),研究了一陣子之後找到類似的東西。


前台長這樣,用Panel裝4個linkbutton,因為設定超過12筆才需要分頁,所以一開始是隱藏的
<asp:Panel ID="PanelPager" runat="server" Visible="false">
        <center>
            <table>
                <tr>
                <td><asp:LinkButton ID="lblFirstPage" runat="server" OnCommand="IndexChanging" CommandArgument="first">第一頁</asp:LinkButton>
                    &nbsp;</td>
                <td><asp:linkbutton ID="lblPrePage" OnCommand="IndexChanging" CommandArgument="pre" runat="server" >上一頁</asp:linkbutton>
                    &nbsp;</td>
                <td><asp:Label ID="lblCurrentPage" runat="server"></asp:Label>&nbsp;&nbsp;/ <asp:Label ID="lblTotalPage" runat="server"></asp:Label>
                    &nbsp;</td>
                <td><asp:linkbutton ID="lblNextPage" OnCommand="IndexChanging" CommandArgument="next" runat="server" >下一頁</asp:linkbutton>
                    &nbsp;</td>
                <td><asp:linkbutton ID="lblLastPage" OnCommand="IndexChanging" CommandArgument="last" runat="server" >最後一頁</asp:linkbutton></td>
                </tr>
            </table>
        </center>
        </asp:Panel>
後台大概這樣,總的來說就是做一個PagedDataSouce再把資料連結出來的DataTable聯繫給PagedDataSouce,最後利用四個LinkButton來表現分頁效果。
public static PagedDataSource pg = new PagedDataSource(); 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["pageindex"] = "0";
            ShowData();            
        }
    }
    protected void ShowData() 
    {
        Panel_Show.Visible = true;
        Entity.Papers ePapers = new Entity.Papers();
        ePapers.Pt_id = "bill";
        DataTable DT = DataRepository.PapersProvider.SearchCount(ePapers);

        //當資料超過12筆才需要分頁
        if (DT != null && DT.Rows.Count > 12)
        {
            PanelPager.Visible = true;
            DataView dv = DT.DefaultView;
            pg.DataSource = dv;

            pg.AllowPaging = true;
            pg.PageSize = 12;
            pg.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());

            //第一頁、最後一頁的設定
            if (!pg.IsFirstPage)
            {
                lblPrePage.Enabled = true;
                lblFirstPage.Enabled = true;
            }
            else
            {
                lblPrePage.Enabled = false;
                lblFirstPage.Enabled = false;
            }
            if (!pg.IsLastPage)
            {
                lblNextPage.Enabled = true;
                lblLastPage.Enabled = true;
            }
            else
            {
                lblNextPage.Enabled = false;
                lblLastPage.Enabled = false;
            }
            int pageindex = Convert.ToInt16(ViewState["pageindex"]);
            pageindex = pageindex + 1;
            lblCurrentPage.Text = (pageindex).ToString();
            lblTotalPage.Text = (pg.PageCount).ToString();
            dltPapersShow.DataSource = pg;
            dltPapersShow.DataBind();
        }
        else
        {
            dltPapersShow.DataSource = DT;
            dltPapersShow.DataBind();
        }
    }
    //資料連結
    public static object GetData(Entity.Papers aPapers)  
    {
        string SQLStr = "SELECT name FROM papers_type WHERE pt_id=@pt_id";
        object Data = SQLDataAccess.ExecuteScalar(SQLDataAccess.DefaultConnectionString, CommandType.Text, SQLStr, new SqlParameter("@pt_id", aPapers.Pt_id));
        return Data;            
    }

做完大概像這樣
擷取   

沒有留言:

張貼留言