学了这么久的越来越发现net比java简单很多虽然从程序的实现上C#和java几乎就是相同的从写程序的时间来看要比javaweb要快很多可以这么说使用学习java的方法学习或者c#是不错的选择java就没那么多的控件可以给我们使用了上次我写过一篇怎么使用repeater控件的文章这次就接着上次的文章来做个listview控件来做个分页显示数据的小程序
准备好一个数据集
新建一个aspx页面打开设计视图先把objectdatasource控件拖拽进去配置一下数据源
把listview拖拽进来选择下数据源
在listview里手动配置下模板这里就用表格显示好了
[html]
<asp:ListView ID=ListView runat=server DataSourceID=ObjectDataSource ItemContainerID= ItemPlaceHolder>
<LayoutTemplate>
<table border= bordercolor=#ff width= bordercollapse=collapse;>
<thead>
<tr>
<td>用户名</td>
<td>密码</td>
</tr>
</thead>
<tbody>
<asp:PlaceHolder runat=server ID=ItemPlaceHolder></asp:PlaceHolder>
</tbody>
</table>
<asp:DataPager runat=server ID=ContactsDataPager PageSize=>
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton=true ShowLastPageButton=true
FirstPageText=首页 LastPageText=尾页
NextPageText=下一页 PreviousPageText=上一页/>
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%#Eval(FNAME)%></td>
<td><%#Eval(FPASSWORD)%></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:PlaceHolder runat=server ID=ItemPlaceHolder></asp:PlaceHolder>这一句实际上是用来占位的ID必须和ItemContainerID保持一致否则会出现下面的错误
这里有种可选的分页分别是
NextPreviousPagerFieldNumericPagerFieldTemplatePagerField
几个重要参数如下
pagesize:每页显示的记录数
ButtonCount:显示的分页数例如如果有页但是ButtonCount=那么可见的页数就是页
PreviousPageText:上一页的显示文本
NextPageText:下一页的显示文本
FirstPageText:首页的显示文本
LastPageText:末页的显示文本
下面是我写的几种分页模板
[html]
<asp:NextPreviousPagerField ShowFirstPageButton=true ShowLastPageButton=true
FirstPageText=首页 LastPageText=尾页
NextPageText=下一页 PreviousPageText=上一页/>
<asp:NumericPagerField ButtonCount= PreviousPageText=上一页 NextPageText=下一页 />
<asp:TemplatePagerField>
<PagerTemplate>
<b>
第
<asp:Label runat=server ID=CurrentPageLabel
Text=<%# ContainerTotalRowCount> ? (ContainerStartRowIndex / ContainerPageSize) + : %> />
页 共
<asp:Label runat=server ID=TotalPagesLabel
Text=<%# MathCeiling ((double)ContainerTotalRowCount / ContainerPageSize) %> />页
(
共<asp:Label runat=server ID=TotalItemsLabel
Text=<%# ContainerTotalRowCount%> />
条记录)
<br />
</b>
</PagerTemplate>
</asp:TemplatePagerField>
<asp:NextPreviousPagerField
ButtonType=Button
ShowFirstPageButton=true
ShowNextPageButton=false
ShowPreviousPageButton=false />
<asp:NumericPagerField
PreviousPageText=< Prev
NextPageText=Next >
ButtonCount= />
<asp:NextPreviousPagerField
ButtonType=Button
ShowLastPageButton=true
ShowNextPageButton=false
ShowPreviousPageButton=false />
数据显示效果
下面是aspx页面的全部代码
[html] <%@ Page Language=C# AutoEventWireup=true CodeBehind=indexaspxcs Inherits=repeaterindex %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head runat=server>
<title>无标题页</title>
<style>
table
{
bordercollapse:collapse;
}
</style>
</head>
<body>
<form id=form runat=server>
<asp:ObjectDataSource ID=ObjectDataSource runat=server
DeleteMethod=Delete InsertMethod=Insert
OldValuesParameterFormatString=original_{} SelectMethod=GetData
TypeName=repeatersourcesM_STUDENTDataSetTableAdaptersM_STUDENTTableAdapter
UpdateMethod=Update>
<DeleteParameters>
<asp:Parameter Name=Original_FID Type=Int />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name=FNAME Type=String />
<asp:Parameter Name=FPASSWORD Type=String />
<asp:Parameter Name=Original_FID Type=Int />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name=FNAME Type=String />
<asp:Parameter Name=FPASSWORD Type=String />
</InsertParameters>
</asp:ObjectDataSource>
<div>
<asp:ListView ID=ListView runat=server DataSourceID=ObjectDataSource ItemContainerID= ItemPlaceHolder>
<LayoutTemplate>
<table border= bordercolor=#ff width= bordercollapse=collapse;>
<thead>
<tr>
<td>用户名</td>
<td>密码</td>
</tr>
</thead>
<tbody>
<asp:PlaceHolder runat=server ID=ItemPlaceHolder></asp:PlaceHolder>
</tbody>
</table>
<asp:DataPager runat=server ID=ContactsDataPager PageSize=>
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton=true ShowLastPageButton=true
FirstPageText=首页 LastPageText=尾页
NextPageText=下一页 PreviousPageText=上一页/>
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%#Eval(FNAME)%></td>
<td><%#Eval(FPASSWORD)%></td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>
<%@ Page Language=C# AutoEventWireup=true CodeBehind=indexaspxcs Inherits=repeaterindex %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head runat=server>
<title>无标题页</title>
<style>
table
{
bordercollapse:collapse;
}
</style>
</head>
<body>
<form id=form runat=server>
<asp:ObjectDataSource ID=ObjectDataSource runat=server
DeleteMethod=Delete InsertMethod=Insert
OldValuesParameterFormatString=original_{} SelectMethod=GetData
TypeName=repeatersourcesM_STUDENTDataSetTableAdaptersM_STUDENTTableAdapter
UpdateMethod=Update>
<DeleteParameters>
<asp:Parameter Name=Original_FID Type=Int />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name=FNAME Type=String />
<asp:Parameter Name=FPASSWORD Type=String />
<asp:Parameter Name=Original_FID Type=Int />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name=FNAME Type=String />
<asp:Parameter Name=FPASSWORD Type=String />
</InsertParameters>
</asp:ObjectDataSource>
<div>
<asp:ListView ID=ListView runat=server DataSourceID=ObjectDataSource ItemContainerID= ItemPlaceHolder>
<LayoutTemplate>
<table border= bordercolor=#ff width= bordercollapse=collapse;>
<thead>
<tr>
<td>用户名</td>
<td>密码</td>
</tr>
</thead>
<tbody>
<asp:PlaceHolder runat=server ID=ItemPlaceHolder></asp:PlaceHolder>
</tbody>
</table>
<asp:DataPager runat=server ID=ContactsDataPager PageSize=>
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton=true ShowLastPageButton=true
FirstPageText=首页 LastPageText=尾页
NextPageText=下一页 PreviousPageText=上一页/>
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%#Eval(FNAME)%></td>
<td><%#Eval(FPASSWORD)%></td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>
代码我上传到资源里大家自己下载吧有问题可以写评论给