在datalist中显示一些datalist行的子行

时间:2012-05-06 02:22:54

标签: asp.net sql-server-2008 datalist

我正在尝试开发一些用户论坛类型页面。我有一个主要论坛主题绑定的数据主义者。它的工作。现在我需要在特定主题下面显示回复或建议(datalist中的特定行)。它将是这样的

数据列表

主题1

回复1

回复2

回复3

主题2

回复1

回复2

回复3

像这样。

我已经在datalist中绑定了主题。主题的表名是alltopics。对任何主题的回复都存储在另一个名为tblreply的表中。我已经在datalist中绑定了主题。但不知道如何做其他部分。任何人都可以帮助我

这是我用于datalist的代码

       Sub binddata5()
                    Dim mycommand As New SqlCommand("SELECT * from alltopics", con)

        con.Open()
        topics.DataSource = mycommand.ExecuteReader
        topics.DataBind()
        con.Close()

datalist design

         <asp:DataList ID="topics" runat="server" DataKeyField="id" 
              RepeatColumns="1">
                 <ItemTemplate>
                 <div>&nbsp&nbsp<strong><asp:Label
                      ID="detail" runat="server" Text='<%#Container.DataItem("topicdetails")%>'></asp:Label></strong>&nbsp&nbsp on &nbsp&nbsp<asp:Label ID="date" runat="server" Text='<%#Container.DataItem("ondate")%>'></asp:Label></td></tr>
                    </div>

                          </ItemTemplate>
                 </asp:DataList>

1 个答案:

答案 0 :(得分:0)

HTML MARK UP

<div>
    <asp:DataList ID="DataList1" runat="server" DataKeyField="bill_id" 
        DataSourceID="SqlDataSource1" OnItemDataBound="loaditems">
        <ItemTemplate>
            bill_id:
            <asp:Label ID="bill_idLabel" runat="server" Text='<%# Eval("bill_id") %>' />
            <br />
            bill_date:
            <asp:Label ID="bill_dateLabel" runat="server" Text='<%# Eval("bill_date") %>' />
            <br />
            bill_del_date:
            <asp:Label ID="bill_del_dateLabel" runat="server" 
                Text='<%# Eval("bill_del_date") %>' />
            <br />
            bill_pat_id:
            <asp:Label ID="bill_pat_idLabel" runat="server" 
                Text='<%# Eval("bill_pat_id") %>' />
            <br />
            <asp:DataList ID="DataList2" runat="server" DataKeyField="test_id">
                <ItemTemplate>
                    test_id:
                    <asp:Label ID="test_idLabel" runat="server" Text='<%# Eval("test_id") %>' />
                    <br />
                    <br />
                </ItemTemplate>
            </asp:DataList>
            <br />
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Diagnosticsql %>" 
        SelectCommand="SELECT * FROM [BILL]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Diagnosticsql %>"             
        SelectCommand="SELECT * FROM [TEST] WHERE ([test_bill_id] like @test_bill_id)">
        <SelectParameters>
            <asp:Parameter Name="test_bill_id" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
</div>  

背后的代码

Protected Sub loaditems(ByVal sender As Object, ByVal e As DataListItemEventArgs) Handles DataList1.ItemDataBound
    If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim billidlabel As Label = CType(e.Item.FindControl("bill_idLabel"), Label)
        Dim servicelist As DataList = CType(e.Item.FindControl("DataList2"), DataList)
        Me.SqlDataSource2.SelectParameters("test_bill_id").DefaultValue = billidlabel.Text
        servicelist.DataSourceID = SqlDataSource2.ID
    End If
End Sub