c# - 如何根据父级的当前 ID 将我的嵌套中继器绑定(bind)到其父级?

标签 c# asp.net sql-server repeater nested-repeater

我在 SQL Server 中有 2 个表。一张表列出了所有 super 英雄,另一张表列出了能力。目前在运行时子转发器获取表中的所有项目,而它应该只获取与父表相关的项目。在我的 aspx 页面上,我有一个嵌套的转发器,如下所示:

<asp:Repeater id="rptHero" runat="server" DataSourceID="sdsHeros" OnItemDataBound="rptHero_ItemDataBound">
    <ItemTemplate>
        <table>
            <tr>
            <td> <%# Eval("HeroName")%> </td>                        
                <asp:Repeater id="rptAbility" runat="server" >                 
                  <ItemTemplate>                     
                    <tr>
                        <td> <%# Eval("AbilityName")%> </td>                     
                    </tr>
                  </ItemTemplate>
               </asp:Repeater>               
            </tr>
        </table>
        <p>&nbsp;</p>            
    </ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="sdsHero" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer2 %>" 
    SelectCommand="SELECT [num], [HeroName] FROM [Super_Heros]"></asp:SqlDataSource>
<asp:SqlDataSource ID="sdsAbility" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer2 %>" 
    SelectCommand="SELECT [num], [AbilityName] FROM [Super_Ability]"></asp:SqlDataSource>

在我后面的代码中有:

protected void rptHero_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;

        Repeater nestedRepeater = e.Item.FindControl("rptAbility") as Repeater;
        nestedRepeater.DataSource = sdsAbility;
        nestedRepeater.DataBind();
    }
}

正在关注 Example 1Example 2由我们的优秀赞助人在 Stack Overflow 上发布,这应该有效,但是,我认为我跳过了 [num] 的部分父转发器的与 [HeroID] 进行比较在 SQLJoin 中的子转发器。

最佳答案

这是我的结局

<asp:Repeater id="repMenu1" runat="server" OnItemDataBound="repMenu1_ItemDataBound">
    <ItemTemplate>
        <table id="gradient-style">
        <tr>
            <th> <%#DataBinder.Eval(Container.DataItem, "Hero")%> </th>
        </tr>
        <asp:Repeater id="repMenu2" runat="server">
            <ItemTemplate>
                <tr>
                <td> <a href="ScoreGraphs.aspx?id=<%#DataBinder.Eval(Container.DataItem, "num")%>"> <%#DataBinder.Eval(Container.DataItem, "Abilities")%></a> </td>
                </tr>       
            </ItemTemplate>
        </asp:Repeater>
        </table>     
    </ItemTemplate>
</asp:Repeater>

在后面的代码中

protected void Page_Load(object sender, EventArgs e)
{
    GetSQLData();
}

//my custom function to get data and create relationship between 2 tables and 
//bind the data on page load so that when menu1 is databound, it will find the matching submenu       
//items in the database and bind them to the second (nested) repeater, 
//displaying the sub-menu items below the parent

protected void GetSQLData()
{
    using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer2"].ConnectionString))
    {
        conn1.Open();
        SqlDataAdapter cmd = new SqlDataAdapter(@"SELECT * FROM [Abilities]; SELECT * FROM [Heros]", conn1);
        DataSet ds = new DataSet();
        cmd.Fill(ds);
        ds.Relations.Add(new DataRelation("nestThem", ds.Tables[0].Columns["num"], ds.Tables[1].Columns["AbilityID"]));

        repMenu1.DataSource = ds;
        repMenu1.DataBind();
    }
}

//Binding the data
protected void repMenu1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataRowView dv = e.Item.DataItem as DataRowView;
    if (dv != null)
    {
        Repeater repSubMenu = e.Item.FindControl("repMenu2") as Repeater;
        if (repSubMenu != null)
        {
            repSubMenu.DataSource = dv.CreateChildView("nestThem");
            repSubMenu.DataBind();
        }
    }
}

我发现这个解决方案既简单又干净。

关于c# - 如何根据父级的当前 ID 将我的嵌套中继器绑定(bind)到其父级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26848651/

相关文章:

c# - 自定义 Intellisense Presenter 问题

asp.net - 在 C# 中引用虚拟目录

c# - 在 xml 文件中嵌入图像

c# - Entity Framework 7 自引用表返回 null

java sql server 获取错误

c# - 使用 .NET 从启动的命令行应用程序中抑制命令窗口

c# - 在 C# 中自动完成电子邮件地址和/或名称

sql-server - 使用 'where then Union' 或使用 'Union then Where'

c# - 关闭对话框窗体关闭父窗体

c# - 'HttpRequest' 不包含 'Params' 的定义