ASP.NET-嵌套中继器

本文关键字:中继器 嵌套 NET- ASP | 更新日期: 2023-09-27 18:28:46

我有一个中继器,用于由"Update_ID"标识的不同更新。每个"Update_ID"都有许多与其相关的图像。

因此,我决定在中继器内为图像嵌套一个中继器进行更新。

问题是,即使有数据显示,图像中继器也永远不会出现。

以下是ASP.NET中的代码:

<asp:Repeater ID="RepeaterUpdates" runat="server" onitemcommand="RepeaterUpdates_ItemCommand">
    <ItemTemplate>
        <div style="border: thin solid #808080">
            <table id="TableUpdates_Repeater" runat="server" style="width:100%; margin:auto; background-image:url(Resources/Icons/white-background.gif)">
                <tr>
                    <td style="width:25%">
                        <br />
                        <asp:Label ID="LabelUpdateID_Repeater" runat="server" Text="Update ID" Enabled="false"></asp:Label>
                        <asp:TextBox ID="TextBoxUpdateID_Repeater" runat="server" Width="50px" Text='<%# Eval("Update_ID") %>' Enabled="false"></asp:TextBox>
                    </td>
                </tr>
        </table>
        <asp:Repeater ID="RepeaterImages" runat="server">
            <ItemTemplate>
                <label>Hello</label>
                <asp:TextBox Text='<%# DataBinder.Eval(Container.DataItem,"Image_ID") %>' runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:Repeater>
        </div>
    </ItemTemplate>
</asp:Repeater>

这是背后的代码:

protected void RepeaterUpdates_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        SqlConnection conn5 = new SqlConnection(connString);
        SqlDataReader rdr5;
        RepeaterItem item = e.Item;
        TextBox Update_ID = (TextBox)item.FindControl("TextBoxUpdateID_Repeater");
        try
        {
            conn5.Open();
            SqlCommand cmd5 = new SqlCommand("SelectImages", conn5);
            cmd5.CommandType = CommandType.StoredProcedure;
            cmd5.Parameters.Add(new SqlParameter("@update_id", Update_ID.Text));
            rdr5 = cmd5.ExecuteReader();
            if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
            {
                Repeater ImageRepeater = (Repeater)item.FindControl("RepeaterImages");
                ImageRepeater.DataSource = rdr5;
                ImageRepeater.DataBind();
            }
        }
        finally
        {
            conn5.Close();
        }
}

如前所述,即使有数据要显示,子中继器也不会出现。请问我该如何解决这个问题?感谢

ASP.NET-嵌套中继器

调用OnItemDataBound 而不是onitemcommand

RepeaterCommandEventArgs更改为RepeaterItemEventArgs

除了@Curt。下面是代码。

代码隐藏

class Images
{
    public int Image_ID;
}
protected void RepeaterUpdates_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    RepeaterItem item = e.Item;
    TextBox Update_ID = (TextBox)item.FindControl("TextBoxUpdateID_Repeater");
    try
    {
        conn5.Open();
        using (SqlCommand cmd5 = new SqlCommand("SelectImages", conn5))
        {
            cmd5.CommandType = CommandType.StoredProcedure;
            cmd5.Parameters.Add(new SqlParameter("@update_id", Update_ID.Text));
            List<Images> Lst = new List<Images>();
            using (SqlDataReader rdr5 = cmd5.ExecuteReader())
            {
                while (rdr5.Read())
                {
                    Lst.Add(new Images { Image_ID = Convert.ToInt16(rdr5["Image_ID"]) });
                }
                if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
                {
                    Repeater ImageRepeater = (Repeater)item.FindControl("RepeaterImages");
                    ImageRepeater.DataSource = Lst;
                    ImageRepeater.DataBind();
                }
            }
        }
    }
    finally
    {
        conn5.Close();
    }
}

HTML

您应该注册ItemBoundData事件

<asp:Repeater ID="rp" runat="server" onitemdatabound="rp_ItemDataBound">
    <ItemTemplate></ItemTemplate>
</asp:Repeater>
protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
}