0

I have the following situation:

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate> 
        <asp:ImageButton OnClick="imgSearchResult_Click" BackColor="#333333" ID="imgSearchResult" height="32" width="32" runat="server" ToolTip='<%# Eval("ToolTip") %>' ImageUrl='<%# Eval("ImageUrl") %>'/> 
        <asp:Label ID="labValue" Text='<%# Eval("Text") %>' runat="server" Width="32" />
    </ItemTemplate>
</asp:Repeater>

The Repeater contains ImageButton and Label objects. If I only use one object, I can add them from codebehind through the binding:

Repeater.DataSource = imageList;
Repeater.DataBind();

OR

Repeater.DataSource = labelList;
Repeater.DataBind();

How can I add both to one Repeater?

2
  • 1
    How are the imageList and labelList related? Commented Jan 25, 2016 at 8:05
  • Not at all, I just need them in one Repeater. Commented Jan 25, 2016 at 8:22

1 Answer 1

1

If you need them in one Repeater, they -are- related in that "view" (loosely used term) of the data. So why not make that view into an explicit object that contains both the image and the label? Either named, or anonymous:

return Enumerable.Zip(imageList, labellist, (image, label) => new {image, label}) 

Alternatively, you can just pass the images and query for the labels by index:

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate> 
        <asp:Label ID="labValue" Text='<%# GetLabelText(Container.ItemIndex) %>' runat="server" Width="32" />
    </ItemTemplate>
</asp:Repeater>

This requires a public GetLabelText(index) method and probably extra state in your code behind to make the ImageList available to that method. Not very nice, but it gets the job done.

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

1 Comment

I see. That seems to work quite well. Thank you very much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.