ASP.NET - 嵌套中继器

时间:2012-03-21 16:54:03

标签: c# asp.net repeater

我有一个转发器,用于“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();
        }
}

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

2 个答案:

答案 0 :(得分:4)

而不是onitemcommand,请致电OnItemDataBound

RepeaterCommandEventArgs更改为RepeaterItemEventArgs

答案 1 :(得分:0)

除了@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)
{

}