首先我们来看下如何使用gridview实现一个master-detail主从关系的应用,以实现一对多的关系,因为这是十分普遍的web应用。在asp.net 1.1中,可能要编写比较多的代码以实现这样的应用,但在asp.net 2.0中,已经可以很方便地实现这样的主从关系的应用了。下面分步来介绍:
我们以sql server 2000中的northwind数据库为例子进行介绍。该数据库中存在很多一对多关系的例子。这里,我们以products表和order detail表予以介绍。其中,我们实现这样的一个应用,通过dropdownlist下拉框,当用户每次选择一种商品时,可以马上通过gridview显示在所有的订单中,有哪些订单中曾经订购了该商品,两表构成典型的一对多关系。
首先,我们要先将商品从product表中取出来,并绑定到dropdownlist中去。我们拖拉一个sqldatasource控件到窗体中去,命名为productListingDataSource,然后设置将product表的productid,productname字段取出,并按照productname进行排序,然后再拖拉一个dropdownlist控件到窗体中,点选该控件的smart tag智能标记,在弹出的菜单中,选择data source为刚才加入的sqldatasource的名称(就是productListingDataSource),并选择productname作为在下拉框中要显示的文本,同时,要将prodcutid设置为下拉框的选定值。如下图所示:
接下来,我们再拖拉一个sqldatasource控件到窗体中去,命名为orderDetailsForProduct,在这个控件中,我们将其绑定到order detail表中,由于不需要返回所有的字段,所以我们只需要返回orderid,unitprice和quantity三个字段就可以了,并且我们需要与dropdownlist构成关联,所以我们要设置适当的sql语句.在asp.net 2.0中,这个也可以通过菜单的操作予以实现。
我们在设置sqldatasource属性的时候,当遇到如下图所示的步骤时,点选"where"按钮.
在点"where"按钮后,在弹出的菜单中(如下图所示),设置column为productid, 设置操作符operator为"=",设置source下拉框的值为control,表示要与来自控件的值进行绑定,选择后,再在右边的parameter properties属性窗口中的conrol id设置为刚才我们添加dropdownlist控件名的名称,最后,记得按"add"按钮,这样,就设置好sql语句了.
最后,增加一个gridview控件,将其于刚才添加的名为orderDetailsForProduct的数据源控件绑定.这样就大功告成了.运行后,结果如下图所示:
可以看到,当选择dropdownlist中的不同商品时,gridview会显示涉及该商品的不同订单的详细资料.程序的代码如下:
<form id="form1" runat="server"> <div> <h2>You are Viewing Order Detail Information for Ordersthat Have Included Shipments of the Selected Product</h2> <asp:SqlDataSource ID="productListingDataSource" Runat="server" ConnectionString= "<%$ ConnectionStrings:NWConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName] FROM [Products]"> </asp:SqlDataSource> <asp:DropDownList ID="productSelector" Runat="server" DataSourceID="productListingDataSource" DataTextField="ProductName" DataValueField="ProductID" AutoPostBack="True"> </asp:DropDownList> <asp:SqlDataSource ID="orderDetailsForProduct" Runat="server" SelectCommand="SELECT [OrderID], [ProductID], [UnitPrice], [Quantity] FROM [Order Details] WHERE ([ProductID] = @ProductID)" ConnectionString= "<%$ ConnectionStrings:NWConnectionString%>" DataSourceMode="DataReader"> <SelectParameters> <asp:ControlParameter Name="ProductID" Type="Int32" ControlID="productSelector" PropertyName="SelectedValue"></asp:ControlParameter> </SelectParameters> </asp:SqlDataSource><asp:GridView ID="orderDetailsGridView" Runat="server" DataSourceID="orderDetailsForProduct" AutoGenerateColumns="False" DataKeyNames="OrderID" BorderWidth="1px" BackColor="LightGoldenrodYellow" GridLines="None" CellPadding="2" BorderColor="Tan" ForeColor="Black"> <FooterStyle BackColor="Tan"></FooterStyle> <PagerStyle ForeColor="DarkSlateBlue" HorizontalAlign="Center" BackColor="PaleGoldenrod"> </PagerStyle> <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle> <AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle> <Columns> <asp:BoundField ReadOnly="True" HeaderText="Order ID" InsertVisible="False" DataField="OrderID" SortExpression="OrderID"> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="Quantity" DataField="Quantity" SortExpression="Quantity" DataFormatString="{0:d}"> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> </Columns> <SelectedRowStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedRowStyle></asp:GridView></div></form>
一起学吧部分文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与一起学吧进行文章共享合作。