ListView選擇自動(dòng)分頁(yè)時(shí) 其實(shí)就是添加了一個(gè)DataPager分頁(yè)控件兩者間存在著嵌套關(guān)系《Repeater與ListView》中提到這樣的分頁(yè)并不是高效的 因?yàn)閿?shù)據(jù)源還是返回了所有的數(shù)據(jù) 而非當(dāng)前頁(yè)數(shù)據(jù)
優(yōu)化方案及步驟:
1.改數(shù)據(jù)源EnablePaging屬性為true 【允許分頁(yè)】
設(shè)置MaximumRowsParameterName="rowIndex"【MSDN解釋?zhuān)涸搮?shù)接受檢索的行數(shù)的值 可以理解為:上一頁(yè)的最后一行的下標(biāo)】
設(shè)置StartRowIndexParameterName="pageSize"【MSDN解釋?zhuān)涸搮?shù)接受要檢索的第一行索引的值 可以理解為pageSize 即每頁(yè)顯示條數(shù)】
SelectCountMethod="GetTotalRowsCount" 【需要總行數(shù)數(shù)時(shí)執(zhí)行的方法即一共有多少條數(shù)據(jù)告訴分頁(yè)控件如何顯示】
2、此時(shí)數(shù)據(jù)源調(diào)用的原有方法getAllClasses不再滿(mǎn)足要求需要在業(yè)務(wù)層中新增一個(gè)帶MaximumRowsParameterName及StartRowIndexParameterName參數(shù)名稱(chēng)的方法 以及GetTotalRowsCount兩個(gè)方法
BLL層添加如下:
復(fù)制代碼 代碼如下:
View Code
public List MODEL.Classes > getPageListByPage( int pageSize, int rowIndex) { return dal.getPageListByPage(pageSize, rowIndex, false);
}
public int GetTotalRowsCount() {
return dal.GetTotalRowsCount();
}
DAL層添加如下:
復(fù)制代碼 代碼如下:
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新增方法如下:
復(fù)制代碼 代碼如下:
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ù)集合 加入到 查詢(xún)命令對(duì)象中
sda.SelectCommand.Parameters.AddRange(pars);
//設(shè)置 查詢(xún)命令類(lèi)型 為存儲(chǔ)過(guò)程
sda.SelectCommand.CommandType = CommandType .StoredProcedure;
//執(zhí)行存儲(chǔ)過(guò)程
sda.Fill(dtcalss);
//執(zhí)行完后 將存儲(chǔ)過(guò)程 獲得的 兩個(gè)輸出參數(shù)值 賦給此方法的兩個(gè)輸出參數(shù)
rowCount = Convert .ToInt32(pars[2].Value);
pageCount = Convert .ToInt32(pars[3].Value);
}
return dtcalss;
}
存儲(chǔ)過(guò)程up_GetPageData2代碼如下:
復(fù)制代碼 代碼如下:
View Code
create proc up_GetPageData2
@LastRowIndex int , ---上一頁(yè)的最后一行的下標(biāo)
@pgSize float , --頁(yè)容量
@rowCount int output, --- 輸出總行數(shù)
@pgCount int output, --- 輸出 總頁(yè)數(shù)
@isDel bit --數(shù)據(jù)是否刪除
as
begin
select @rowCount =count (*) from classes where cisdel= @isDel --查出總行數(shù)
set @pgCount =ceiling ( @rowCount/ @pgSize )-- 算出總頁(yè)數(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代碼如下:
復(fù)制代碼 代碼如下:
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取消"開(kāi)啟分頁(yè)"自動(dòng)分頁(yè) 拖入分頁(yè)控件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>
您可能感興趣的文章:- android開(kāi)發(fā)教程之listview顯示sqlite數(shù)據(jù)
- Android利用listview控件操作SQLite數(shù)據(jù)庫(kù)實(shí)例
- Android ListView數(shù)據(jù)綁定顯示的三種解決方法
- android實(shí)現(xiàn)listview分頁(yè)的方法
- sqlite查詢(xún)結(jié)果在listview中展示的實(shí)現(xiàn)