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

如何实现ListView高效分页代码

程序员文章站 2024-03-03 18:42:40
listview选择自动分页时  其实就是添加了一个datapager分页控件两者间存在着嵌套关系《repeater与listview》中提到这样的分页并不是高效...

listview选择自动分页时  其实就是添加了一个datapager分页控件两者间存在着嵌套关系《repeater与listview》中提到这样的分页并不是高效的 因为数据源还是返回了所有的数据  而非当前页数据 

优化方案及步骤:

1.改数据源enablepaging属性为true 【允许分页】

设置maximumrowsparametername="rowindex"【msdn解释:该参数接受检索的行数的值  可以理解为:上一页的最后一行的下标】

设置startrowindexparametername="pagesize"【msdn解释:该参数接受要检索的第一行索引的值  可以理解为pagesize 即每页显示条数】

selectcountmethod="gettotalrowscount" 【需要总行数数时执行的方法即一共有多少条数据告诉分页控件如何显示】

如何实现ListView高效分页代码

2、此时数据源调用的原有方法getallclasses不再满足要求需要在业务层中新增一个带maximumrowsparametername及startrowindexparametername参数名称的方法  以及gettotalrowscount两个方法

bll层添加如下:

复制代码 代码如下:

view code

public list <model.classes > getpagelistbypage( int pagesize, int rowindex) {            return dal.getpagelistbypage(pagesize, rowindex, false);
        }

        public int gettotalrowscount() {
            return dal.gettotalrowscount();
        }


dal层添加如下:

复制代码 代码如下:

view code

public list <model. classes> getpagelistbypage( int rowindex, int pagesize, bool isdel) {            int rowcount = 0;
            int pagecount = 0;
            datatable dt = sqlhelper .getpagelistbypage(rowindex, pagesize, out rowcount, out pagecount, isdel);
            if (dt.rows.count > 0) {
                list <model.classes > list = new list <model.classes >();
                foreach (datarow dr in dt.rows) {
                    model. classes model = new model. classes();
                    loadentitydata(model, dr);
                    list.add(model);
                }
                return list;
            }
            return null ;
        }

        public int gettotalrowscount() {
            string sqlstr = "select * from classes where cisdel = 0" ;
            return sqlhelper .executescalar(sqlstr);
        }


sqlhelper新增方法如下:

复制代码 代码如下:

view code

public static datatable getpagelistbypage( int rowindex, int pagesize, out int rowcount, out int pagecount, bool isdel) {            datatable dtcalss = new datatable();
            rowcount = 0;
            pagecount = 0;
            using (sqlconnection sqlcon = new sqlconnection (connstr)) {
                sqldataadapter sda = new sqldataadapter( "up_getpagedata2" , sqlcon);
                sqlparameter [] pars = {
                                      new sqlparameter ( "@lastrowindex",rowindex),
                                      new sqlparameter ( "@pgsize",pagesize),
                                        new sqlparameter ( "@rowcount",rowcount),
                                        new sqlparameter ( "@pgcount",pagecount),
                                        new sqlparameter ( "@isdel",isdel),
                                      };
                //将两个输出参数的输出方向指定
                pars[2].direction = parameterdirection .output;
                pars[3].direction = parameterdirection .output;
                //将参数集合 加入到 查询命令对象中
                sda.selectcommand.parameters.addrange(pars);
                //设置 查询命令类型 为存储过程
                sda.selectcommand.commandtype = commandtype .storedprocedure;
                //执行存储过程
                sda.fill(dtcalss);
                //执行完后 将存储过程 获得的 两个输出参数值 赋给此方法的两个输出参数
                rowcount = convert .toint32(pars[2].value);
                pagecount = convert .toint32(pars[3].value);
            }
            return dtcalss;
        }


存储过程up_getpagedata2代码如下:

复制代码 代码如下:

view code

create proc up_getpagedata2
@lastrowindex int , ---上一页的最后一行的下标
@pgsize float , --页容量
@rowcount int output, --- 输出总行数
@pgcount int output, --- 输出 总页数
@isdel   bit --数据是否删除
as
begin
      select @rowcount =count (*) from classes where cisdel= @isdel --查出总行数
      set @pgcount =ceiling ( @rowcount/ @pgsize )-- 算出总页数
      select * from (
          select row_number () over ( order by cid ) as rnum, * from classes where cisdel= @isdel
      ) as temp
      where rnum >@lastrowindex and rnum <= @lastrowindex +@pgsize
end


listview.aspx代码如下:

复制代码 代码如下:

view code

<% @ page language="c#" autoeventwireup="true" codebehind="listview.aspx.cs" inherits ="webform.listview" %>
<! 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>
</ head>
< body>
    <form id="form1" runat="server">
    <div >

        < asp: objectdatasource id ="objectdatasource1" runat ="server"
            selectmethod ="getpagelistbypage" typename ="bll.classes"
            dataobjecttypename ="model.classes" deletemethod ="softdel" insertmethod ="add"
            updatemethod ="modify" enablepaging ="true"
            maximumrowsparametername ="rowindex" selectcountmethod ="gettotalrowscount"
            startrowindexparametername ="pagesize">
        </ asp: objectdatasource >
        < asp: listview id ="listview1" runat ="server" datasourceid ="objectdatasource1"
            insertitemposition ="lastitem">
            < alternatingitemtemplate>
                < tr style ="">
                    < td>
                        < asp: button id ="deletebutton" runat ="server" commandname ="delete" text ="删除" />
                        < asp: button id ="editbutton" runat ="server" commandname ="edit" text ="编辑" />
                    </ td>
                    < td>
                        < asp: label id ="cidlabel" runat ="server" text =' <%# eval("cid") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="cnamelabel" runat ="server" text =' <%# eval("cname") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="ccountlabel" runat ="server" text =' <%# eval("ccount") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="cimglabel" runat ="server" text =' <%# eval("cimg") %> ' />
                    </ td>
                    < td>
                        < asp: checkbox id ="cisdelcheckbox" runat ="server"
                            checked ='<% # eval("cisdel") %> ' enabled ="false" />
                    </ td>
                    < td>
                        < asp: label id ="caddtimelabel" runat ="server" text =' <%# eval("caddtime") %> ' />
                    </ td>
                </ tr>
            </ alternatingitemtemplate>

            < edititemtemplate>
                < tr style ="">
                    < td>
                        < asp: button id ="updatebutton" runat ="server" commandname ="update" text ="更新" />
                        < asp: button id ="cancelbutton" runat ="server" commandname ="cancel" text ="取消" />
                    </ td>
                    < td>
                        < asp: textbox id ="cidtextbox" runat ="server" text =' <%# bind("cid") %> ' />
                    </ td>
                    < td>
                        < asp: textbox id ="cnametextbox" runat ="server" text =' <%# bind("cname") %> ' />
                    </ td>
                    < td>
                        < asp: textbox id ="ccounttextbox" runat ="server" text =' <%# bind("ccount") %> ' />
                    </ td>
                    < td>
                        < asp: textbox id ="cimgtextbox" runat ="server" text =' <%# bind("cimg") %> ' />
                    </ td>
                    < td>
                        < asp: checkbox id ="cisdelcheckbox" runat ="server"
                            checked ='<% # bind("cisdel") %> ' />
                    </ td>
                    < td>
                        < asp: textbox id ="caddtimetextbox" runat ="server"
                            text ='<% # bind("caddtime") %> ' />
                    </ td>
                </ tr>
            </ edititemtemplate>
            < emptydatatemplate>
                < table runat ="server"

                    style ="">
                    < tr>
                        < td>
                            未返回数据。 </ td>
                    </ tr>
                </ table>
            </ emptydatatemplate>
            < insertitemtemplate>
                < tr style ="">
                    < td>
                        < asp: button id ="insertbutton" runat ="server" commandname ="insert" text ="插入" />
                        < asp: button id ="cancelbutton" runat ="server" commandname ="cancel" text ="清除" />
                    </ td>
                    < td>
                        < asp: textbox id ="cidtextbox" runat ="server" text =' <%# bind("cid") %> ' />
                    </ td>
                    < td>
                        < asp: textbox id ="cnametextbox" runat ="server" text =' <%# bind("cname") %> ' />
                    </ td>
                    < td>
                        < asp: textbox id ="ccounttextbox" runat ="server" text =' <%# bind("ccount") %> ' />
                    </ td>
                    < td>
                        < asp: textbox id ="cimgtextbox" runat ="server" text =' <%# bind("cimg") %> ' />
                    </ td>
                    < td>
                        < asp: checkbox id ="cisdelcheckbox" runat ="server"
                            checked ='<% # bind("cisdel") %> ' />
                    </ td>
                    < td>
                        < asp: textbox id ="caddtimetextbox" runat ="server"
                            text ='<% # bind("caddtime") %> ' />
                    </ td>
                </ tr>
            </ insertitemtemplate>
            < itemtemplate>
                < tr style ="">
                    < td>
                        < asp: button id ="deletebutton" runat ="server" commandname ="delete" text ="删除" />
                        < asp: button id ="editbutton" runat ="server" commandname ="edit" text ="编辑" />
                    </ td>
                    < td>
                        < asp: label id ="cidlabel" runat ="server" text =' <%# eval("cid") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="cnamelabel" runat ="server" text =' <%# eval("cname") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="ccountlabel" runat ="server" text =' <%# eval("ccount") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="cimglabel" runat ="server" text =' <%# eval("cimg") %> ' />
                    </ td>
                    < td>
                        < asp: checkbox id ="cisdelcheckbox" runat ="server"
                            checked ='<% # eval("cisdel") %> ' enabled ="false" />
                    </ td>
                    < td>
                        < asp: label id ="caddtimelabel" runat ="server" text =' <%# eval("caddtime") %> ' />
                    </ td>
                </ tr>
            </ itemtemplate>
            < layouttemplate>
                < table runat ="server">
                    < tr runat ="server">
                        < td runat ="server">
                            < table id ="itemplaceholdercontainer" runat ="server" border ="0"

                                style ="">
                                < tr runat ="server" style ="">
                                    < th runat ="server">
                                        </ th>
                                    < th runat ="server">
                                        cid </ th>
                                    < th runat ="server">
                                        cname </ th>
                                    < th runat ="server">
                                        ccount </ th>
                                    < th runat ="server">
                                        cimg </ th>
                                    < th runat ="server">
                                        cisdel </ th>
                                    < th runat ="server">
                                        caddtime </ th>
                                </ tr>
                                < tr id ="itemplaceholder" runat ="server">
                                </ tr>
                            </ table>
                        </ td>
                    </ tr>
                    < tr runat ="server">
                        < td runat ="server"

                            style ="">
                        </ td>
                    </ tr>
                </ table>
            </ layouttemplate>
            < selecteditemtemplate>
                < tr style ="">
                    < td>
                        < asp: button id ="deletebutton" runat ="server" commandname ="delete" text ="删除" />
                        < asp: button id ="editbutton" runat ="server" commandname ="edit" text ="编辑" />
                    </ td>
                    < td>
                        < asp: label id ="cidlabel" runat ="server" text =' <%# eval("cid") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="cnamelabel" runat ="server" text =' <%# eval("cname") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="ccountlabel" runat ="server" text =' <%# eval("ccount") %> ' />
                    </ td>
                    < td>
                        < asp: label id ="cimglabel" runat ="server" text =' <%# eval("cimg") %> ' />
                    </ td>
                    < td>
                        < asp: checkbox id ="cisdelcheckbox" runat ="server"
                            checked ='<% # eval("cisdel") %> ' enabled ="false" />
                    </ td>
                    < td>
                        < asp: label id ="caddtimelabel" runat ="server" text =' <%# eval("caddtime") %> ' />
                    </ td>
                </ tr>
            </ selecteditemtemplate>
        </ asp: listview >

    </div >
    <asp : datapager id ="datapager1" runat ="server" pagedcontrolid ="listview1"
        pagesize ="5">
        < fields>
            < asp: nextpreviouspagerfield buttontype ="button" showfirstpagebutton ="true"
                showlastpagebutton ="true" />
        </ fields>
    </asp : datapager>
    </form >
</ body>
</ html>


3、界面中listview1取消"开启分页"自动分页  拖入分页控件datapage并设置pagedcontrolid="listview1"使其与listview1建立关联

4、修改数据源调用的方法为getpagelistbypage运行结果如下:

如何实现ListView高效分页代码

补充:

如果运行报错'objectdatasource“objectdatasource1”未能找到带参数的非泛型方法“getpagelistbypage”: pagesize, pageindex。'

只需删除aspx界面中

 <selectparameters>

                <asp:parameter defaultvalue="5" name="pagesize" type="int32" />

                <asp:parameter name="rowindex" type="int32" />

</selectparameters>