一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - 如何實現(xiàn)ListView高效分頁代碼

如何實現(xiàn)ListView高效分頁代碼

2019-10-25 11:37asp.net教程網(wǎng) ASP.NET教程

ListView選擇自動分頁時 其實就是添加了一個DataPager分頁控件兩者間存在著嵌套關系《Repeater與ListView》中提到這樣的分頁并不是高效的 因為數(shù)據(jù)源還是返回了所有的數(shù)據(jù) 而非當前頁數(shù)據(jù)

ListView選擇自動分頁時  其實就是添加了一個DataPager分頁控件兩者間存在著嵌套關系《Repeater與ListView》中提到這樣的分頁并不是高效的 因為數(shù)據(jù)源還是返回了所有的數(shù)據(jù)  而非當前頁數(shù)據(jù) 

優(yōu)化方案及步驟:

1.改數(shù)據(jù)源EnablePaging屬性為true 【允許分頁】

設置MaximumRowsParameterName="rowIndex"【MSDN解釋:該參數(shù)接受檢索的行數(shù)的值  可以理解為:上一頁的最后一行的下標】

設置StartRowIndexParameterName="pageSize"【MSDN解釋:該參數(shù)接受要檢索的第一行索引的值  可以理解為pageSize 即每頁顯示條數(shù)】

SelectCountMethod="GetTotalRowsCount" 【需要總行數(shù)數(shù)時執(zhí)行的方法即一共有多少條數(shù)據(jù)告訴分頁控件如何顯示】

如何實現(xiàn)ListView高效分頁代碼

2、此時數(shù)據(jù)源調(diào)用的原有方法getAllClasses不再滿足要求需要在業(yè)務層中新增一個帶MaximumRowsParameterName及StartRowIndexParameterName參數(shù)名稱的方法  以及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),
                                      };
                //將兩個輸出參數(shù)的輸出方向指定
                pars[2].Direction = ParameterDirection .Output;
                pars[3].Direction = ParameterDirection .Output;
                //將參數(shù)集合 加入到 查詢命令對象中
                sda.SelectCommand.Parameters.AddRange(pars);
                //設置 查詢命令類型 為存儲過程
                sda.SelectCommand.CommandType = CommandType .StoredProcedure;
                //執(zhí)行存儲過程
                sda.Fill(dtcalss);
                //執(zhí)行完后 將存儲過程 獲得的 兩個輸出參數(shù)值 賦給此方法的兩個輸出參數(shù)
                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, --- 輸出總行數(shù)
@pgCount int output, --- 輸出 總頁數(shù)
@isDel   bit --數(shù)據(jù)是否刪除
as
begin
      select @rowCount =count (*) from classes where cisdel= @isDel --查出總行數(shù)
      set @pgCount =ceiling ( @rowCount/ @pgSize )-- 算出總頁數(shù)
      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>
                            未返回數(shù)據(jù)。 </ 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建立關聯(lián)

 

4、修改數(shù)據(jù)源調(diào)用的方法為getPageListByPage運行結果如下:

如何實現(xiàn)ListView高效分頁代碼

補充:

如果運行報錯'ObjectDataSource“ObjectDataSource1”未能找到帶參數(shù)的非泛型方法“getPageListByPage”: pageSize, pageIndex。'

只需刪除aspx界面中

 <SelectParameters>

                <asp:Parameter DefaultValue="5" Name="pageSize" Type="Int32" />

                <asp:Parameter Name="rowIndex" Type="Int32" />

</SelectParameters>

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产片自拍 | 精品国语国产在线对白 | 国产精品福利久久2020 | caopo视频进入离开 | 亚洲精品视频一区 | 国产精品麻豆久久99 | 国产剧情一区二区三区 | 韩国三级在线 | 日韩成人在线视频 | 亚洲黄色成人 | 男人晚上适合偷偷看的污污 | 色姑娘导航 | 2020年最新国产精品视频免费 | 国产成人高清亚洲一区91 | 色先锋av资源中文字幕 | 九九99香蕉在线视频美国毛片 | 天莱男模gary | 日本videos有奶水的hd | 99久久精品免费看国产一区二区 | 日本草草视频在线观看 | 男女羞羞的视频 | 九九99热久久精品在线6 | 天堂在线免费观看 | 草莓视频旧版本 | 啊啊啊好大好爽视频 | 国产精品嫩草影院一二三区 | 国产精品久久久久久久久免费 | 丝瓜视频在线观看污 | 免费日批 | 白丝超短裙被输出娇喘不停小说 | 国产伦精品一区二区三区免 | 色男人网| 青青精品视频 | 特黄未满14周岁毛片 | 久久伊人中文字幕有码 | 国产精品午夜久久 | 亚洲欧美国产精品久久久 | 免费在线观看伦理片 | 欧美一区二区三区高清不卡tv | 精品免费视频 | 日本大学jalapsiki |