4. 产品显示过程
示例4列举列举了产品页的实现代码。
示例4:实现产品显示页面
<%@ Page Language="C#" MasterPageFile="~/Common.master" Title="Products Display" %>示例4中所示的代码与示例3非常类似,除了本页面中的ObjectDataSource控件调用了不同的方法来获取所有产品。
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="Server">
<asp:ObjectDataSource ID="productSource" runat="server"
TypeName="AdventureWorksTraderBiz.ProductBiz"
SelectMethod="GetProducts">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ProductSubcategoryID"
Direction="Input" Name="productSubcategoryID" DefaultValue="1"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:Label runat="server" ID="lblHeading" Font-Size="Medium"
Font-Underline="False"
ForeColor="#0000C0">
List of Products
</asp:Label><br />
<br />
<asp:GridView HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="blue"
HeaderStyle-ForeColor="White" AutoGenerateColumns="False"
ID="gridProducts" runat="server"
DataSourceID="productSource">
<Columns>
<asp:BoundField ReadOnly="True" HeaderText="ProductID"
DataField="ProductID" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Product Number" DataField="ProductNumber" />
<asp:BoundField HeaderText="Color" DataField="Color" />
<asp:BoundField HeaderText="ListPrice" DataField="ListPrice" />
<asp:BoundField HeaderText="Modified Date" HtmlEncode="false" DataFormatString="{0:MM/dd/yyyy}"
DataField="ModifiedDate" />
</Columns>
</asp:GridView>
</asp:Content>
小结
在这个实例中,读者已经学习了各种很好的用于创建高效ASP.NET 2.0 Web站点的实践技巧,包括使用EntLib应用程序块,以及ASP.NET和SQL Server 2005的新功能。该实例所讨论的应用程序说明了将应用程序块集成到真实ASP.NET站点的方法,同时还讲解了泛型集合的使用方法。此外,本实例还讨论了ObjectDataSource控件如何支持分层应用程序设计,从而允许开发人员直接将对象方法的输出绑定到ASP.NET页面中的控件。希望读者在自己的项目中能够因地制宜的应用这些技术特性。