在清单1中,SqlDataSource通过设置ConnectionString 和SelectCommand 属性控制从AdventureWorks数据库的Product表中检索数据,ConnectionString属性通过一个ASP.NET表达式从 web.config文件获取连接字符串,在我的测试机上,连接字符串定义在web.config中,如:
<connectionStrings>
<add name="AdventureWorks"
connectionString="server=localhost;uid=sa;
pwd=thiru;database=AdventureWorks;"/> connectionString="server=localhost;uid=sa;
pwd=thiru;database=AdventureWorks;"/>
</connectionStrings>
设置好SqlDataSource属性后,下一步是通过ListView控件显示数据,下面是在LayoutTemplate模板中的标记:
<LayoutTemplate>
> style="width:460px">
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
<asp:DataPager runat="server" ID="DataPager" PageSize="3">
<Fields>
<asp:NumericPagerField ButtonCount="10"
PreviousPageText="< PreviousPageText="<--" NextPageText="-->" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
LayoutTemplate模板定义了ListView控件输出内容的容器,除了在ListView控件顶层定义了table 外,LayoutTemplate模板还定义了<asp:DataPager>,它为ListView控件提供了分页功能,DataPager让你可以为任何数据绑定控件实现IpageableItemContainer进行数据分页并显示导航控制。
有两种方法使数据分页(DataPager)和数据绑定(data-bound)联合使用:
1、设置DataPager 的PagedControlID属性为data-bound的名字。
2、将DataPager置于data-bound层次体系之下,对于ListView控件,你可以将DataPager置于LayoutTemplate组件内。
设置DataPager的PageSize属性,它控制每页显示的数据行数,你也可以在页面提交到服务器时通过设置QueryStringField属性实现。
在DataPager内,你指定NumericPageField模板,它可以让用户输入一个页号,然后按照页号进行跳转,如:
<asp:NumericPagerField ButtonCount="10"
PreviousPageText="< PreviousPageText="<--"
NextPageText="-->" />
ItemTemplate组件为每个记录的明细提供了标记。图1显示了在浏览器中导航到该页面的输出。
图1.ListView示例:通过数据绑定ListView控件到SqlDataSource控件检索Product表中部分数据产生的输出