欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.net中使用PageDataSource分页实现代码

程序员文章站 2024-03-03 23:06:28
复制代码 代码如下:注:封装数据绑定控件(如 system.web.ui.webcontrols.datagrid、system.web.ui.webcontrols.gr...

复制代码 代码如下:

注:封装数据绑定控件(如 system.web.ui.webcontrols.datagrid、system.web.ui.webcontrols.gridview、system.web.ui.webcontrols.detailsview
    //     和 system.web.ui.webcontrols.formview)的与分页相关的属性,以允许该控件执行分页操作。无法继承此类。
datalist
public datatable getdataset(string sql)
    {
        sqlconnection conn = this.getconn();
        sqldataadapter sdr = new sqldataadapter(sql, conn);
        dataset rs = new dataset();
        sdr.fill(rs);
        return rs.tables[0];
    }
    public pageddatasource pagedatalistbind(string sql, int currentpage, int pagesize)
    {
        pageddatasource pds = new pageddatasource();
        pds.datasource = getdataset(sql).defaultview;
        pds.allowpaging = true;
        pds.pagesize = pagesize;
        pds.currentpageindex = currentpage - 1;
        return pds;
    }

 
db db = new db();
    pageddatasource pds = new pageddatasource();
    protected void page_load(object sender, eventargs e)
    {
        if (!ispostback)
        {

            bind();
        }
    }

    public void bind()
    {
        pds = db.pagedatalistbind("select * from tb_word",convert.toint32(lblcurrentpage.text),2);  
        lnkbtnfirst.enabled = true;
        lnkbtnlast.enabled = true;
        lnkbtnprevious.enabled = true;
        lnkbtnnext.enabled = true;
        if (lblcurrentpage.text == "1")
        {
            lnkbtnfirst.enabled = false;
            lnkbtnprevious.enabled = false;
        }
        if(lblcurrentpage.text==pds.pagecount.tostring())
        {
            lnkbtnlast.enabled = false;
            lnkbtnnext.enabled = false;
        }
        lblsumpage.text = pds.pagecount.tostring();
        datalist1.datasource = pds;
        datalist1.datakeyfield = "id";
        datalist1.databind();
    }

    protected void lnkbtnfirst_click(object sender, eventargs e)
    {
        lblcurrentpage.text = "1";
        bind();
    }
    protected void lnkbtnprevious_click(object sender, eventargs e)
    {
        lblcurrentpage.text = (convert.toint32(lblcurrentpage.text) - 1).tostring();
        bind();
    }
    protected void lnkbtnnext_click(object sender, eventargs e)
    {
        lblcurrentpage.text = (convert.toint32(lblcurrentpage.text) + 1).tostring();
        bind();
    }
    protected void lnkbtnlast_click(object sender, eventargs e)
    {
        lblcurrentpage.text = lblsumpage.text;
        bind();
    }

<form id="form1" runat="server">
    <div>
        <asp:datalist id="datalist1" runat="server">
        <itemtemplate>
            <table border="1">
                <tr>
                    <td>id</td><td>标题</td><td>内容</td>
                </tr>
                <tr>
                    <td><%#eval("id") %></td>
                    <td><%#eval("title") %></td>
                    <td><%#eval("content") %></td>
                </tr>
            </table>
        </itemtemplate>
        </asp:datalist>
        当前页码为[<asp:label id="lblcurrentpage" runat="server" text="1"></asp:label>]页
                总页码[<asp:label
                    id="lblsumpage" runat="server" text="0"></asp:label>]页
                <asp:linkbutton id="lnkbtnfirst" runat="server" font-underline="false" onclick="lnkbtnfirst_click" > 第一页 </asp:linkbutton>
                <asp:linkbutton id="lnkbtnprevious" runat="server" font-underline="false" onclick="lnkbtnprevious_click" > 上一页 </asp:linkbutton>
                <asp:linkbutton id="lnkbtnnext" runat="server" font-underline="false" onclick="lnkbtnnext_click" > 下一页 </asp:linkbutton>
                <asp:linkbutton id="lnkbtnlast" runat="server" font-underline="false" onclick="lnkbtnlast_click"> 末一页 </asp:linkbutton>
    </div>
    </form>