最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

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

來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:41:44
文檔

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

如何實(shí)現(xiàn)ListView高效分頁代碼:ListView選擇自動(dòng)分頁時(shí) 其實(shí)就是添加了一個(gè)DataPager分頁控件兩者間存在著嵌套關(guān)系《Repeater與ListView》中提到這樣的分頁并不是高效的 因?yàn)閿?shù)據(jù)源還是返回了所有的數(shù)據(jù) 而非當(dāng)前頁數(shù)據(jù) 優(yōu)化方案及步驟: 1.改數(shù)據(jù)源EnablePaging屬性為tru
推薦度:
導(dǎo)讀如何實(shí)現(xiàn)ListView高效分頁代碼:ListView選擇自動(dòng)分頁時(shí) 其實(shí)就是添加了一個(gè)DataPager分頁控件兩者間存在著嵌套關(guān)系《Repeater與ListView》中提到這樣的分頁并不是高效的 因?yàn)閿?shù)據(jù)源還是返回了所有的數(shù)據(jù) 而非當(dāng)前頁數(shù)據(jù) 優(yōu)化方案及步驟: 1.改數(shù)據(jù)源EnablePaging屬性為tru

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

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

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

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

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

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

2、此時(shí)數(shù)據(jù)源調(diào)用的原有方法getAllClasses不再滿足要求需要在業(yè)務(wù)層中新增一個(gè)帶MaximumRowsParameterName及StartRowIndexParameterName參數(shù)名稱的方法  以及GetTotalRowsCount兩個(gè)方法

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),
                                      };
                //將兩個(gè)輸出參數(shù)的輸出方向指定
                pars[2].Direction = ParameterDirection .Output;
                pars[3].Direction = ParameterDirection .Output;
                //將參數(shù)集合 加入到 查詢命令對(duì)象中
                sda.SelectCommand.Parameters.AddRange(pars);
                //設(shè)置 查詢命令類型 為存儲(chǔ)過程
                sda.SelectCommand.CommandType = CommandType .StoredProcedure;
                //執(zhí)行存儲(chǔ)過程
                sda.Fill(dtcalss);
                //執(zhí)行完后 將存儲(chǔ)過程 獲得的 兩個(gè)輸出參數(shù)值 賦給此方法的兩個(gè)輸出參數(shù)
                rowCount = Convert .ToInt32(pars[2].Value);
                pageCount = Convert .ToInt32(pars[3].Value);
            }
            return dtcalss;
        }

存儲(chǔ)過程up_GetPageData2代碼如下:

代碼如下:
View Code

create proc up_GetPageData2
@LastRowIndex int , ---上一頁的最后一行的下標(biāo)
@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>
                            未返回?cái)?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取消"開啟分頁"自動(dòng)分頁  拖入分頁控件DataPage并設(shè)置PagedControlID="ListView1"使其與ListView1建立關(guān)聯(lián)

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

補(bǔ)充:

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

只需刪除aspx界面中

 <SelectParameters>

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

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

</SelectParameters>

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

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

如何實(shí)現(xiàn)ListView高效分頁代碼:ListView選擇自動(dòng)分頁時(shí) 其實(shí)就是添加了一個(gè)DataPager分頁控件兩者間存在著嵌套關(guān)系《Repeater與ListView》中提到這樣的分頁并不是高效的 因?yàn)閿?shù)據(jù)源還是返回了所有的數(shù)據(jù) 而非當(dāng)前頁數(shù)據(jù) 優(yōu)化方案及步驟: 1.改數(shù)據(jù)源EnablePaging屬性為tru
推薦度:
標(biāo)簽: 實(shí)現(xiàn) 代碼 高效
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top