程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> 如何實現ListView高效分頁代碼

如何實現ListView高效分頁代碼

編輯:ASP.NET基礎

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

優化方案及步驟:

1.改數據源EnablePaging屬性為true 【允許分頁】

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

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

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

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運行結果如下:

補充:

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

只需刪除aspx界面中

 <SelectParameters>

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

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

</SelectParameters>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved