0

I have a repeater for different updates identified by "Update_ID". Each "Update_ID" has a number of images associated to it.

Therefore, I decided to nest a repeater for the images inside the repeater for updates.

The problem is that the image repeater never shows up, even if there is data to show.

Here is the code in 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>

Here is the code-behind:

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();
        }
}

As previously stated, the child repeater never shows up, even if there is data to display. How can I solve this problem please? Thanks

3
  • Have you tried debugging? Does your code pass the right parameter values to the nested repeater data source? Commented Mar 21, 2012 at 16:56
  • 1
    "Have you tried debugging?" may be the funniest thing I've ever seen. Commented Mar 21, 2012 at 16:57
  • You should do that in ItemDataBound instead of ItemCommand. You could change visibility in ItemCommand if you want. But it must be ensured that the inner repeater is created in page load the latest for every outer RepeaterItem which is the case with ItemDataBound. Commented Mar 21, 2012 at 17:01

2 Answers 2

4

Rather than onitemcommand, call OnItemDataBound

Change RepeaterCommandEventArgs to RepeaterItemEventArgs

Sign up to request clarification or add additional context in comments.

4 Comments

I tried your suggestion but the following error crops up: Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0123: No overload for 'RepeaterUpdates_ItemCommand' matches delegate 'System.Web.UI.WebControls.RepeaterItemEventHandler'
@Matthew I've updated my answer. You need to change your method parameter type
@Curt I tried your suggestion. However, in the repeater event hanlder I have other code such as: if(e.CommandName.Equals("Delete")) which does not work after making the changes :s
I agree. I see nothing in the parent itemTemplate that would trigger the onItemCommand. Or @Matthew should insert a control (button, linkbutton, ...) in the parent ItemTemplate that would trigger the onItemCommand and show only the images for the item.
0

In addition to @Curt. Below is the code.

Code Behind

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

You should Register ItemBoundData Event

<asp:Repeater ID="rp" runat="server" onitemdatabound="rp_ItemDataBound">
    <ItemTemplate></ItemTemplate>
</asp:Repeater>

protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.