欢迎光临
我们一直在努力

如何使用PagedDataSource來做DataRepeater的分頁的效果-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

asp.net中的datalist和datarepeater提供了簡單快速的方法來展現資料,

其<itemtemplate>更是讓我們能隨心所欲的決定資料的排放方式.可惜的是他

們不像datagrid那樣,有內建的分頁功能.

如何解決這個不能分頁的問題呢?在這篇文章中將會介紹如何使用 pageddatasource class 來解決分頁的問題.

簡單的介紹幾個常用到的 pageddatasource class public properties

datasource – 資料來源

allowpaging – true 允許分頁;false 不允許.

pagesize – 決定重複多少次

pagecount – 總共有幾頁

currentpageindex – 目前所在的頁數

isfirstpage – 是第一頁嗎,回傳true or false

islastpage – 是最後一頁嗎,回傳true or false

下面就是範例(剪貼就可以直接跑了)

——————————————————————————–

<%@ page language="vb" %>

<%@ import namespace="system.data" %>

<script runat="server">

sub page_load(sender as object, e as eventargs)

dim pgds as pageddatasource = new pageddatasource()

pgds.datasource = createdatasource().defaultview

pgds.allowpaging = true

pgds.pagesize = 6 決定重複多少次

lbltotalpage.text = pgds.pagecount.tostring()

dim currentpage as integer

if not request.querystring("page") is nothing then

currentpage=convert.toint32(request.querystring("page"))

else

currentpage=1

end if

pgds.currentpageindex = currentpage-1

lblcurrentpage.text = "page: " + currentpage.tostring()

if not pgds.isfirstpage then

lnkprev.navigateurl=request.currentexecutionfilepath + "?page=" + convert.tostring(currentpage-1)

end if

if not pgds.islastpage then

lnknext.navigateurl=request.currentexecutionfilepath+ "?page=" + convert.tostring(currentpage+1)

end if

repeater1.datasource=pgds

repeater1.databind()

end sub

function createdatasource() as datatable

this part is an example from asp.net quickstart

you can change this part with your own dataset

dim dt as datatable

dim dr as datarow

dim i as integer

create a datatable

dt = new datatable

dt.columns.add(new datacolumn("integervalue", gettype(integer)))

dt.columns.add(new datacolumn("stringvalue", gettype(string)))

dt.columns.add(new datacolumn("datetimevalue", gettype(datetime)))

dt.columns.add(new datacolumn("boolvalue", gettype(boolean)))

make some rows and put some sample data in

for i = 0 to 50

dr = dt.newrow()

dr(0) = i

dr(1) = "item " + i.tostring()

dr(2) = datetime.now.toshorttimestring

if (i mod 2 <> 0) then

dr(3) = true

else

dr(3) = false

end if

add the row to the datatable

dt.rows.add(dr)

next

return dt

end function

</script>

<html><head>

<title>datarepeater paging example</title>

<style type=text/css>

body {

font: 10px verdana, arial, helvetica, "sans serif"; color: #000000;

}

.txt {

font-size: 12px

}

</style>

</head>

<body>

<form name=form1 method=post runat="server">

<table class=txt width="100%" border=0>

<tbody><tr><td>

<asp:hyperlink id=lnkprev runat="server"><< prev</asp:hyperlink>

<asp:hyperlink id=lnknext runat="server">next >></asp:hyperlink>

<asp:label id=lblcurrentpage runat="server"></asp:label>

of <asp:label id=lbltotalpage runat="server"></asp:label>

</td></tr></tbody></table>

<asp:repeater id=repeater1 runat="server">

<itemtemplate>

<hr align="left" width="60%" size="1">

<table class=txt width="100%" border="0">

<tr>

<td>

order date: <%# databinder.eval(container.dataitem, "datetimevalue", "{0:d}") %>

</td><tr><td>

quantity: <%# databinder.eval(container.dataitem, "integervalue", "{0:n2}") %>

</td><tr><td>

item: <%# databinder.eval(container.dataitem, "stringvalue") %>

</td><tr><td>

order date: <asp:checkbox id=chk1 checked=<%# databinder.eval(container.dataitem, "boolvalue") %> runat="server"/>

</td></tr></table>

</itemtemplate>

</asp:repeater>

<hr hight="1">

<p>datasource:microsoft quickstart example

http://cht.gotdotnet.com/quickstart/aspplus/doc/webdatabinding.aspx

</p>

<p>reference:msdn .net framework class library

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolspageddatasourceclasstopic.asp

</p><p>

paging with repeater control in asp.net

http://www.charon.co.uk/content.aspx?categoryid=28&articleid=21 </p></form></body></html>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何使用PagedDataSource來做DataRepeater的分頁的效果-.NET教程,Asp.Net开发
分享到: 更多 (0)