转发器内的转发器 - 父ID

时间:2013-08-30 19:48:56

标签: c# asp.net repeater

Repeater in Repeater是我的问题的答案。

但是,由于我需要使用方法来获取数据,因此我对childRepeater.DataSource = ...;感到困惑,并且该方法使用参数parent ID。我不明白我是如何获得parent ID的值 - 外部转发器的ID。

我看了几个其他的例子,但没有答案可行......

2 个答案:

答案 0 :(得分:2)

使用引用的答案,它将是这样的,当然只检查空值。

protected void ItemBound(object sender, RepeaterItemEventArgs args)
    {
        if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");
            Repeater parentRepeater = (Repeater)args.Item.FindControl("ParentRepeater");       
            childRepeater.DataSource = YourMethod(parentRepeater.ID);
            childRepeater.DataBind();
        }
    }

答案 1 :(得分:1)

enter image description here

<asp:Repeater ID="ParentRepeater" runat="server"    
   OnItemDataBound="ParentRepeater_ItemDataBound">
    <ItemTemplate>
        <strong>Parent</strong>
        <asp:Label runat="server" ID="FirstNameLabel" 
        Text='<%# Eval("FirstName") %>' />
        <asp:Label runat="server" ID="LastNameLabel" 
        Text='<%# Eval("LastName") %>' />
        <br/>
        <!-- Repeated data -->
        <asp:Repeater ID="ChildRepeater" runat="server">
            <ItemTemplate>
                <!-- Nested repeated data -->
                <strong>Children</strong>
                <asp:Label runat="server" ID="FirstNameLabel" 
                Text='<%# Eval("FirstName") %>' />
                <asp:Label runat="server" ID="LastNameLabel" 
                Text='<%# Eval("LastName") %>' /><br/>
            </ItemTemplate>
        </asp:Repeater>
        <hr/>
    </ItemTemplate>
</asp:Repeater>

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public List<User> Children;

    public User()
    {
        Children = new List<User>();
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var users = new List<User>
        {
            new User
            {
                UserId = 1,
                FirstName = "Jon",
                LastName = "Doe",
                Children = new List<User> {new User {
                  UserId = 3, FirstName = "Marry", LastName = "Doe"}}
            },
            new User
            {
                UserId = 2,
                FirstName = "Eric",
                LastName = "Newton",
                Children = new List<User> {new User {
                  UserId = 3, FirstName = "Nick", LastName = "Newton"}}
            }
        };

        ParentRepeater.DataSource = users;
        ParentRepeater.DataBind();
    }
}

protected void ParentRepeater_ItemDataBound(object sender, 
    RepeaterItemEventArgs args)
{
    if (args.Item.ItemType == ListItemType.Item || 
       args.Item.ItemType == ListItemType.AlternatingItem)
    {
        var user = args.Item.DataItem as User;
        var childRepeater = (Repeater) args.Item.FindControl("ChildRepeater");

        // You can get parent id like this - 
        // int parentUserId = user.UserId;

        childRepeater.DataSource = user.Children;
        childRepeater.DataBind();
    }
}