2

我在数据库中有 2 个表 w/c 已连接

tblPackage
id Name
1  Package A
2  Package B

tblDetails
id PackageID Details
1  1         PackageDetails11
2  1         PackageDetails12
3  1         PackageDetails13
4  1         PackageDetails14
5  2         PackageDetails21
6  2         PackageDetails22
7  2         PackageDetails23

现在我希望它在中继器上操纵它

HTML

<asp:Repeater ID="rptrPackage" runat="server">
<ItemTemplate>
    <asp:Label id="Pack" runat="server" Text='<%# Bind("Pack") %>'></asp:Label>
    <asp:BulletedList id="Details" runat="server">
    </asp:BulletedList>
    </ItemTemplate>
</asp:Repeater>

ASP

private void Populate()
{
    DataTable dtPackage = tblPackage();
    DataTable dtDetails = tblDetails();

    rptrPackage.DataSource = dtPackage;
    rptrPackage.DataBind();
}
4

2 回答 2

1

试试这个

aspx

    <asp:Repeater ID="rptrPackage" runat="server">
        <ItemTemplate>
            <asp:Label ID="Pack" runat="server" Text='<%# Bind("Pack") %>'></asp:Label>
            <asp:HiddenField ID="hID" Value='<%# Eval("PackageID") %>' runat="server" />
            <asp:Repeater runat="server" ID="rptDetails">
                <HeaderTemplate>
                    <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <%# Eval("Details")%>
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                    </ul>
                </FooterTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>

后面的代码

    private void Populate()
    {
        DataTable dtPackage = tblPackage();
        DataTable dtDetails = tblDetails();

        rptrPackage.DataSource = dtPackage;
        rptrPackage.DataBind();

        foreach (RepeaterItem item in rptrPackage.Items)
        {
            Repeater rptDetails = ((Repeater)item.FindControl("rptDetails"));
            rptDetails.DataSource = dtDetails.Select("PackageID = " + ((HiddenField)item.FindControl("hID")).Value).CopyToDataTable();
            rptDetails.DataBind();
        }
    }
于 2013-08-10T05:24:04.470 回答
0

使用SqlDataSourse内部中继器,

<asp:Repeater ID="rptrPackage" runat="server" >
<ItemTemplate>
<asp:HiddenField ID="PackID" runat="server"  Value='<%# Eval("id") %>' 
/>
<asp:Label id="Pack" runat="server" Text='<%# Eval("Name") %>'></asp:Label><br/>
<asp:BulletedList ID="Details" runat="server"   
        DataSourceID="SqlDataSource1" DataTextField="Details"   
        DataValueField="id">
        </asp:BulletedList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"    ConnectionString="<%$ ConnectionStrings:YourConnectionString %>" 
    SelectCommand="SELECT * FROM [tblDetail] WHERE [PackageID] = @PID"  >
    <SelectParameters>
        <asp:ControlParameter    Name="PID"   Type="Int32"  ControlID="PackID"   PropertyName="Value" />
     </SelectParameters>
 </asp:SqlDataSource>
 </ItemTemplate>
</asp:Repeater>
于 2013-08-10T05:32:07.413 回答