ASP.NET 3.5中使用新的ListView控件

2008-08-25 14:04:09.0     浏览:2715     来源:中国IT实验室
关键词:  ASP.NET 3.5     ListView控件  








There are no products!


DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:AdventureWorks%>"
SelectCommand="SELECT ProductID,Name,ProductNumber,
Color,ListPrice FROM Production.Product">






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


connectionString="server=localhost;uid=sa;
pwd=thiru;database=AdventureWorks;"/>

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


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

你已经看到LisView控件支持的多个模板了,下一步是要创建一个简单的web站点,名字就叫做ListViewExample(你可以从http://assets.devx.com/sourcecode/38579_tt_mainsource.zip下载该站点的示例代码),创建好web站点后,选择Web站点?添加新项目,添加一个新的ASP.NET页面,名字命名为SimpleListView.aspx(见清单1),这个页面将使用ListView控件从AdventureWorks示例数据库中的Product表显示产品数据。

清单1.ListView控件示例清单


<%@ Page Language="C#" %>
http://www.w3.org/1999/xhtml">


Simple Data Binding Example using ListView control




DataSourceID="productSource" DataKeyNames="ProductID">

style="width:460px">





PreviousPageText="<--" NextPageText="-->" />





Product ID : Text=''<%#Eval("ProductID") %>'' />


Name : Text=''<%#Eval("Name") %>'' />


Product Number : runat="server" Text=''<%#Eval("ProductNumber") %>'' />

--------------------------------------------------------
------------------
style="width:460px">





PreviousPageText="<--" NextPageText="-->" />


LayoutTemplate模板定义了ListView控件输出内容的容器,除了在ListView控件顶层定义了table外,LayoutTemplate模板还定义了,它为ListView控件提供了分页功能,DataPager让你可以为任何数

[上一页]   [第1页]   [第2页]   [第3页]   [第4页]   [第5页]   [第6页]   [第7页]   [下一页]