首页 > 开发 > 综合 > 正文

DataList和DataRepeater分页

2024-07-21 02:23:42
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,asp.net中的datalist和datarepeater提供了简单快速的方法來显示,其间,我们可以使用<itemtemplate>更是使我们能随心所欲的控制数据的排放样式!.可惜的是它们不像datagrid那样,有内置的分页功能。

如何解决呢?

其实我们可以【pageddatasource】 类来解决分页的问题。

pageddatasource类的属性:


datasource -数据源

allowpaging - true 是否允许分页.
pagesize - 每页项目数量
pagecount - 总页数

currentpageindex - 当前所在的页索引
代码如下:

<%@ page language="vb" %>
<%@ import namespace="system.data" %>
<script runat="server">
sub page_load(byval sender as object, byval 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 = 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

dim dt as datatable
dim dr as datarow
dim i as integer

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)))


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

dt.rows.add(dr)
next

return dt

end function

</script>

<html><head>
<title>datarepeater</title>
<style type=text/css>

body {
font: 10px verdana, arial, helvetica, "sans serif"; color: #000000;
}
.txt {
font-size: 12px
}
</style>
</head>
<body>
<form id="form1" name=form1 method=post runat="server">
<table class=txt width="100%" border=0>
<tbody><tr><td>

<asp:hyperlink id=lnkprev runat="server">上页</asp:hyperlink>
<asp:hyperlink id=lnknext runat="server">下页</asp:hyperlink>第
<asp:label id=lblcurrentpage runat="server"></asp:label> 页 共 <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">

</form>

</body>

</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表