简体   繁体   中英

ASP.net C# Repeater multiple objects

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM