中继器中的中继器

发布于 2024-09-03 22:04:34 字数 276 浏览 2 评论 0原文

我的中继器里面有一个中继器。其中父中继器绑定到一个 Datatble,其中有一列包含 Datatable

我想将子中继器绑定到父中继器数据行中的数据表列

这可能吗?我想我可以直接在 aspx 文件中执行此操作,例如:

DataSource="<%# DataBinder.Eval(Container.DataItem, "Products")%>" 但它似乎不起作用。

I have a repeater inside a repeater. Where the parent repeater is bound to a Datatble which has a column with a Datatable in it.

I would like to bind the child repeater to the datatable column in the parent repeater's datarow

Is this possible? i was thinking i could do this directly in the aspx file like:

DataSource="<%# DataBinder.Eval(Container.DataItem, "Products")%>" but it doesn't seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拔了角的鹿 2024-09-10 22:04:34

在父中继器中,将一个方法附加到 OnItemDataBound 事件,并在该方法中找到嵌套的中继器并对其进行数据绑定。

示例 (.aspx):

<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ItemBound">
    <ItemTemplate>
        <!-- Repeated data -->
        <asp:Repeater ID="ChildRepeater" runat="server">
            <ItemTemplate>
                <!-- Nested repeated data -->
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

示例 (.cs):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ParentRepeater.DataSource = ...;
        ParentRepeater.DataBind();
    }
}

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");
        childRepeater.DataSource = ...;
        childRepeater.DataBind();
    }
}

In the parent repeater, attach a method to the OnItemDataBound event and in the method, find the nested repeater and data bind it.

Example (.aspx):

<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ItemBound">
    <ItemTemplate>
        <!-- Repeated data -->
        <asp:Repeater ID="ChildRepeater" runat="server">
            <ItemTemplate>
                <!-- Nested repeated data -->
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

Example (.cs):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ParentRepeater.DataSource = ...;
        ParentRepeater.DataBind();
    }
}

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");
        childRepeater.DataSource = ...;
        childRepeater.DataBind();
    }
}
孤独患者 2024-09-10 22:04:34

我将向子中继器本身添加一个 DataBinding 事件:

<asp:Repeater ID="parentRepeater" runat="server">
    <asp:Repeater ID="childRepeater" runat="server"
        OnDataBinding="childRepeater_DataBinding" />
</asp:Repeater>

然后只需实现它:

protected void childRepeater_DataBinding(object sender, System.EventArgs e)
{
    Repeater rep = (Repeater)(sender);

    int someIdFromParentDataSource = (int)(Eval("ParentID"));

    // Assuming you have a function call `GetSomeData` that will return
    // the data you want to bind to your child repeater.
    rep.DataSource = GetSomeData(int);
    rep.DataBind();
}

我更喜欢在控件级别而不是 ItemDataBound 级别执行此操作,这样如果您必须删除其中的控件或项目,您的模板中,您不必担心在使用它的父控件中查找代码。一切都由他自己控制。另外,您永远不必执行FindControl

如果您想将来替换某个控件,只需将其删除,您的代码仍然可以工作,因为它是完全独立的。使用 ItemDataBound 会导致您的代码仍然可以编译,但由于它依赖于子控件,因此在运行时崩溃或出现意外行为。

I would add a DataBinding event to the child repeater itself:

<asp:Repeater ID="parentRepeater" runat="server">
    <asp:Repeater ID="childRepeater" runat="server"
        OnDataBinding="childRepeater_DataBinding" />
</asp:Repeater>

Then just implement it:

protected void childRepeater_DataBinding(object sender, System.EventArgs e)
{
    Repeater rep = (Repeater)(sender);

    int someIdFromParentDataSource = (int)(Eval("ParentID"));

    // Assuming you have a function call `GetSomeData` that will return
    // the data you want to bind to your child repeater.
    rep.DataSource = GetSomeData(int);
    rep.DataBind();
}

I prefer to do it at the control level instead of the ItemDataBound level so that if you ever have to remove controls or items within your templates you don't have to worry about looking for code in the parent controls that use it. It get's all localize witht he control itself. Plus you never have to do a FindControl.

If you want to replace a control in the future you can just delete it and your code will still work since it is all self contained. Using the ItemDataBound would cause your code to still compile but crash or act unexpectedly at runtime because of it's reliance on child controls.

野鹿林 2024-09-10 22:04:34

其实现方式如下:

DataSource='<%# ((System.Data.DataRowView)Container.DataItem)[3] %>'

因此,如果您知道父表中保存嵌套转发器的子表/数据源的列,则可以将其直接放入 aspx 文件中。

Here is how it's done:

DataSource='<%# ((System.Data.DataRowView)Container.DataItem)[3] %>'

So if you know the column in the parent table that holds the child table/datasource for the nested repeater you can put this directly in the aspx file.

傻比既视感 2024-09-10 22:04:34

Repeater1 OnItemDataBound 事件,然后是 FindControl Repeater2。 代码隐藏将找不到嵌套的Repeater2!您必须使用FindControl("Repeater2")。

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.DataItem != null)
    {
        MemberView dataRow = (MemberView)e.Item.DataItem;
        var cat = MemberPresenter.getMemberID(dataRow.memID);

        Repeater rp2 = (Repeater)e.Item.FindControl("Repeater2");
        rp2.DataSource = cat;
        rp2.DataBind();
    }  
}

Repeater1 OnItemDataBound event, then FindControl Repeater2. The code-behind will not find the nested Repeater2! You have to use FindControl("Repeater2").

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.DataItem != null)
    {
        MemberView dataRow = (MemberView)e.Item.DataItem;
        var cat = MemberPresenter.getMemberID(dataRow.memID);

        Repeater rp2 = (Repeater)e.Item.FindControl("Repeater2");
        rp2.DataSource = cat;
        rp2.DataBind();
    }  
}
故事还在继续 2024-09-10 22:04:34

如果我需要这样做,我通常使用父中继器的 ItemDataBound 事件来绑定子中继器。如果 e 是您的 EventArgs 参数,您将可以通过 e.Item.FindControl() 访问子中继器,并通过 e.Item.DataItem 访问数据。

If I need to do that, I usually do it using the ItemDataBound event of the parent repeater to bind the child repeater. If e is your EventArgs parameter, you'll have access to the child repeater via e.Item.FindControl(), and access to the data via e.Item.DataItem.

画离情绘悲伤 2024-09-10 22:04:34
 protected void MainRepeater_ItemDataBound(object sender, RepeaterItemEventArgs args)
    {
         if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");

                DataTable innerTable= ((DataRowView)args.Item.DataItem)["InnerTableColumnName"] as DataTable;
                childRepeater.DataSource = tasksDetails;
                childRepeater.DataBind();
            }
    }
 protected void MainRepeater_ItemDataBound(object sender, RepeaterItemEventArgs args)
    {
         if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");

                DataTable innerTable= ((DataRowView)args.Item.DataItem)["InnerTableColumnName"] as DataTable;
                childRepeater.DataSource = tasksDetails;
                childRepeater.DataBind();
            }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文