asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

在ASP.NET 3.5中使用新的ListView控件[2]


发布日期:2020年05月26日
 
在ASP.NET 3.5中使用新的ListView控件[2]

一个简单的数据绑定的例子

你已经看到LisView控件支持的多个模板了下一步是要创建一个简单的web站点名字就叫做ListViewExample(你可以从http://assetsdevxcom/sourcecode/_tt_mainsourcezip下载该站点的示例代码)创建好web站点后选择Web站点&#;添加新项目添加一个新的ASPNET页面名字命名为SimpleListViewaspx(见清单这个页面将使用ListView控件从AdventureWorks示例数据库中的Product表显示产品数据

清单ListView控件示例清单

<%@ Page Language=C# %>

<html xmlns=http://wwwworg//xhtml>

<head runat=server>

<link rel=Stylesheet type=text/css href=StyleSheetcss />

<title>Simple Data Binding Example using ListView control</title>

</head>

<body>

<form id=form runat=server>

<div>

<asp:ListView runat=server ID=productsView

DataSourceID=productSource DataKeyNames=ProductID>

<LayoutTemplate>

<table cellpadding= runat=server id=tblProducts

style=width:px>

<tr runat=server id=itemPlaceholder>

</tr>

</table>

<asp:DataPager runat=server ID=DataPager PageSize=>

<Fields>

<asp:NumericPagerField ButtonCount=

PreviousPageText=< NextPageText=> />

</Fields>

</asp:DataPager>

</LayoutTemplate>

<ItemTemplate>

<tr id=row style=height:px runat=server>

<td valign=top class=ProductInfo>

Product ID : <asp:Label ID=lblProductID runat=server

Text=<%#Eval(ProductID) %> />

<br />

Name : <asp:Label ID=lblName runat=server

Text=<%#Eval(Name) %> />

<br />

Product Number : <asp:Label ID=lblProductNumber

runat=server Text=<%#Eval(ProductNumber) %> />

</td>

</tr>

</ItemTemplate>

<ItemSeparatorTemplate>

<tr id=separator style=height:px runat=server>

<td>

</td>

</tr>

</ItemSeparatorTemplate>

<EmptyDataTemplate>

There are no products!

</EmptyDataTemplate>

</asp:ListView>

<asp:SqlDataSource id=productSource runat=server

DataSourceMode=DataSet

ConnectionString=<%$ ConnectionStrings:AdventureWorks%>

SelectCommand=SELECT ProductIDNameProductNumber

ColorListPrice FROM ProductionProduct>

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

在清单SqlDataSource通过设置ConnectionString 和SelectCommand 属性控制从AdventureWorks数据库的Product表中检索数据ConnectionString属性通过一个ASPNET表达式从webconfig文件获取连接字符串在我的测试机上连接字符串定义在webconfig中

<connectionStrings>

<add name=AdventureWorks

connectionString=server=localhost;uid=sa;

pwd=thiru;database=AdventureWorks;/>

</connectionStrings>

设置好SqlDataSource属性后下一步是通过ListView控件显示数据下面是在LayoutTemplate模板中的标记

<LayoutTemplate>

<table cellpadding= runat=server id=tblProducts

style=width:px>

<tr runat=server id=itemPlaceholder>

</tr>

</table>

<asp:DataPager runat=server ID=DataPager PageSize=>

<Fields>

<asp:NumericPagerField ButtonCount=

PreviousPageText=< NextPageText=> />

</Fields>

</asp:DataPager>

</LayoutTemplate>

LayoutTemplate模板定义了ListView控件输出内容的容器除了在ListView控件顶层定义了table外LayoutTemplate模板还定义了<asp:DataPager>它为ListView控件提供了分页功能DataPager让你可以为任何数据绑定控件实现IpageableItemContainer进行数据分页并显示导航控制

有两种方法使数据分页(DataPager)和数据绑定(databound)联合使用

设置DataPager 的PagedControlID属性为databound的名字

将DataPager置于databound层次体系之下对于ListView控件你可以将DataPager置于LayoutTemplate组件内

设置DataPager的PageSize属性它控制每页显示的数据行数你也可以在页面提交到服务器时通过设置QueryStringField属性实现

在DataPager内你指定NumericPageField模板它可以让用户输入一个页号然后按照页号进行跳转

<asp:NumericPagerField ButtonCount=

PreviousPageText=<

NextPageText=> />

ItemTemplate组件为每个记录的明细提供了标记显示了在浏览器中导航到该页面的输出

ListView示例通过数据绑定ListView控件到SqlDataSource控件检索Product表中部分数据产生的输出

[] [] [] []

               

上一篇:在ASP.NET 3.5中使用新的ListView控件[3]

下一篇:在ASP.NET 3.5中使用新的ListView控件[4]