datalist分页

datalist分页,第1张

Aspnet提供了三个功能强大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分页功能。相对DataGrid,DataList和Repeater控件具有更高的样式自定义性,所以很多时候我们喜欢使用DataList或Repeater控件来显示数据。

实现DataList或Repeater控件的分页显示有几种方法:

1、写一个方法或存储过程,根据传入的页数返回需要显示的数据表(DataTable)

2、使用PagedDataSource类(位于SystemWebUIWebControls命名空间里)

本篇文章主要说怎么使用PagedDataSource类实现DataList和Repeater控件的分页显示。DataGrid控件内部也使用了PagedDataSource类,PagedDataSource 类封装 DataGrid 控件的属性,这些属性使 DataGrid 可以执行分页。

PagedDataSource 类的部分公共属性:

AllowCustomPaging 获取或设置指示是否启用自定义分页的值。

AllowPaging 获取或设置指示是否启用分页的值。

Count 获取要从数据源使用的项数。

CurrentPageIndex 获取或设置当前页的索引。

DataSource 获取或设置数据源。

DataSourceCount 获取数据源中的项数。

FirstIndexInPage 获取页中的第一个索引。

IsCustomPagingEnabled 获取一个值,该值指示是否启用自定义分页。

IsFirstPage 获取一个值,该值指示当前页是否是首页。

IsLastPage 获取一个值,该值指示当前页是否是最后一页。

IsPagingEnabled 获取一个值,该值指示是否启用分页。

IsReadOnly 获取一个值,该值指示数据源是否是只读的。

IsSynchronized 获取一个值,该值指示是否同步对数据源的访问(线程安全)。

PageCount 获取显示数据源中的所有项所需要的总页数。

PageSize 获取或设置要在单页上显示的项数。

VirtualCount 获取或设置在使用自定义分页时数据源中的实际项数。

这些属性是否和DataGrid的属性很相似?没错,DataGrid控件就是使用PagedDataSource类来实现数据分页显示的 。下面举个使用PagedDataSource类实现DataList和Repeater控件的分页显示的例子:

public void Page_Load(Object src,EventArgs e)

{

OleDbConnection objConn=new OleDbConnection(@"Provider=MicrosoftJetOLEDB40; Data Source=c:\testmdb");

OleDbDataAdapter objCommand=new OleDbDataAdapter("select from Users",objConn);

DataSet ds=new DataSet();

objCommandFill(ds);

//对PagedDataSource 对象的相关属性赋值

PagedDataSource objPds = new PagedDataSource();

objPdsDataSource = dsTables[0]DefaultView;

objPdsAllowPaging = true;

objPdsPageSize = 5;

int CurPage;

//当前页面从Page查询参数获取

if (RequestQueryString["Page"] != null)

CurPage=ConvertToInt32(RequestQueryString["Page"]);

else

CurPage=1;

objPdsCurrentPageIndex = CurPage-1;

lblCurrentPageText = "Page: " + CurPageToString();

if (!objPdsIsFirstPage)

lnkPrevNavigateUrl=RequestCurrentExecutionFilePath + "Page=" + ConvertToString(CurPage-1);

if (!objPdsIsLastPage)

lnkNextNavigateUrl=RequestCurrentExecutionFilePath+ "Page=" + ConvertToString(CurPage+1);

//把PagedDataSource 对象赋给Repeater控件

Repeater1DataSource=objPds;

Repeater1DataBind();

}

参照如下代码改下html样式就好 ,不明白的留言我

<table width="100%">

<tr>

<td style="text-align:right">

第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)ContainerParentParent)PageIndex + 1 %>' />页

共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)ContainerParentParent)PageCount %>' />页

<asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />

<asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />

<asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />

<asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />

<asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)ContainerParentParent)PageIndex + 1 %>' />

<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->

</td>

</tr>

</table>

</PagerTemplate>

protected void grdvSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

//thisgrdvSearchResultPageIndex = eNewPageIndex;

////thisDataBind();

DataTable aaa = new DataTable();

aaa = (DataTable)ViewState["TableForAllSelect"];

grdvSearchResultDataSource = aaa;

grdvSearchResultDataBind();

grdvSearchResultVisible = true;

GridView theGrid = sender as GridView; // refer to the GridView

int newPageIndex = 0;

if (-2 == eNewPageIndex)

{ // when click the "GO" Button

TextBox txtNewPageIndex = null;

//GridViewRow pagerRow = theGridControls[0]Controls[theGridControls[0]ControlsCount - 1] as GridViewRow; // refer to PagerTemplate

GridViewRow pagerRow = theGridBottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow

//updated at 2006年6月21日3:15:33

if (null != pagerRow)

{

txtNewPageIndex = pagerRowFindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value

}

if (null != txtNewPageIndex)

{

newPageIndex = intParse(txtNewPageIndexText) - 1; // get the NewPageIndex

}

}

else

{ // when click the first, last, previous and next Button

newPageIndex = eNewPageIndex;

}

// check to prevent form the NewPageIndex out of the range

newPageIndex = newPageIndex < 0 0 : newPageIndex;

newPageIndex = newPageIndex >= theGridPageCount theGridPageCount - 1 : newPageIndex;

// specify the NewPageIndex

theGridPageIndex = newPageIndex;

}

我近来刚做的,给你吧!

前台:放一个datalist控件,名为ListDataList

,四个button控件,分别表示“首页”“前页”“后页”“尾页”,两个label

“CurPageLab”、“PageTotalLab”分别用来显示当前页数和总页数!

后台代码如下:

C#

codepublic

void

BindToList()

{

int

PageTotal;//总页数

int

CurrentPage;//当前页

string

SqlStr

=

"select

from

news

where

classId

='"+

classId+"'";

SqlDataAdapter

sda

=

new

SqlDataAdapter();

sdaSelectCommand

=

new

SqlCommand(SqlStr

,GetConnection());//GetConnection

是一个已经打开的连接,这个方法是自己写的,下面的CloseConnectiom()同

DataSet

ds

=

new

DataSet();

sdaFill(ds,"dsList");

PagedDataSource

ps

=

new

PagedDataSource();//实例化一个PagedDataSource对象,这是实现分页的核心

psAllowPaging

=

true;//允许分页,如果不设置为true,还是不能实现效果的

psPageSize

=

15;//设置每一页显示的条数

psDataSource

=

dsTables["dsList"]DefaultView;

thisListDataListDataSource

=

ps;

CurrentPage

=

ConvertToInt32(thisCurPageLabText);

psCurrentPageIndex

=

CurrentPage

-

1;

PageTotal

=

psPageCount;//“PageCount”属性表示总页数

if

(CurrentPage

==

1)//如果当前是第一页,自然就没有必要去点击“首页”、“前页”了,故将“首页”、“前页”的Enabled属性

设置为

false

{

thisButton1Enabled

=

false;

thisButton2Enabled

=

false;

}

else

{

thisButton1Enabled

=

true;

thisButton2Enabled

=

true;

}

if

(CurrentPage

==

PageTotal))//如果当前是最后一页,自然就没有必要去点击“后页”、“尾页”了,故将“后页”、“尾页”的Enabled属性

设置为

false

{

thisButton3Enabled

=

false;

thisButton4Enabled

=

false;

}

else

{

thisButton3Enabled

=

true;

thisButton4Enabled

=

true;

}

thisPageTotalLabText

=

"共"

+

PageTotalToString()

+

"页";//在页面上放一个label显示总页数

thisListDataListDataBind();//绑定数据

CloseConnectiom();//关闭数据库

}

以上就是关于datalist分页全部的内容,包括:datalist分页、asp.net GridView分页中页脚如何加上当前页和总页数、c#中dataGridView问题高手进!!急!!!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9783102.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存