asp.net 2.0中显示主从关系另一个方法

  一般是在asp.net 2.0中,是用master-detail的关系来显示数据的,显示的办法也很多,但今天
看书发现一个,是在同一个gridview里,搭配bulletedlist来显示的。关键部分如下,同时也有用了ajax
<asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                        AutoGenerateColumns="False" DataKeyNames="类别编号" DataSourceID="SqlDataSource1"
                        Width="696px" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
                        CellPadding="4" PageSize="2" OnRowDataBound="GridView1_RowDataBound">
                        <Columns>
                            <asp:BoundField DataField="类别编号" HeaderText="类别编号" InsertVisible="False" ReadOnly="True"
                                SortExpression="类别编号" />
                            <asp:BoundField DataField="类别名称" HeaderText="类别名称" SortExpression="类别名称" />
                            <asp:BoundField DataField="说明" HeaderText="说明" SortExpression="说明" />
                            <asp:TemplateField HeaderText="产品清单">
                                <ItemTemplate>
                                    <asp:BulletedList ID="BulletedList1" runat="server" DataTextField="产品" DataValueField="产品">
                                    </asp:BulletedList>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                        <RowStyle BackColor="White" ForeColor="#330099" />
                        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                    </asp:GridView>
                    <i>您当前正在查看的页码:<b><font color="#ff0000"><%=GridView1.PageIndex + 1%>
                        /
                        <%=GridView1.PageCount%>
                    </font></b></i>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
                        SelectCommand="SELECT [类别编号], [类别名称], [说明] FROM [产品类别]"></asp:SqlDataSource>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
                        SelectCommand="SELECT [产品], [类别编号] FROM [产品资料]"></asp:SqlDataSource>
                </ContentTemplate>
            </asp:UpdatePanel>

其中用到了两个sqldatasource,一个是选择产品类别,另一个是从产品表中取出产品。
然后在后端代码中,如下处理

DataView MyDataView;

    protected void Page_Load(object sender, EventArgs e)
    {
        MyDataView = (DataView)(this.SqlDataSource2.Select(DataSourceSelectArguments.Empty));
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            BulletedList bl = (BulletedList)(e.Row.FindControl("BulletedList1"));
            MyDataView.RowFilter = "类别编号 = " + ((DataRowView)(e.Row.DataItem))["类别编号"].ToString();
            bl.DataSource = MyDataView;
            bl.DataBind();
        }

   首先从产品表中选出数据集,形成一个dataview,然后在gridview的rowdatabound事件中,
先找出bulledlist控件,然后利用dataview的rowfilter属性,过滤出对应类别的产品,再绑定到具体的datasource控件

你可能感兴趣的:(asp.net)