MaxOvrdrv
MaxOvrdrv

Reputation: 1916

nest repeater with list of string

I have an object of the following type:

public class TriggerMessage
{
    public List<string> Triggers_EN { get; set; }
    public List<string> Triggers_FR { get; set; }
    public string Message_EN { get; set; }
    public string Message_FR { get; set; }
    public int UID { get; set; }
}

Now, I also have a bunch of these objects in a List...

List<TriggerMessage> dataset = new List<TriggerMessage>(){.......};

Now I am also binding this list to a repeater:

<table style="width:100%;">
    <asp:Repeater ID="lstTriggers" runat="server">
        <HeaderTemplate>
            <tr style="border-bottom:1px solid black;">
                <th>English Triggers</th>
                <th>French Triggers</th>
                <th>English Message</th>
                <th>French Message</th>
                <th>Actions</th>
            </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><div style="overflow-x:scroll; width:100%"><%#Eval("Triggers_EN").ToString() %></div></td>
                <td><div style="overflow-x:scroll; width:100%"><%#Eval("Triggers_FR").ToString() %></div></td>
                <td><div style="overflow-x:scroll; width:100%"><%#Eval("Message_EN") %></div></td>
                <td><div style="overflow-x:scroll; width:100%"><%#Eval("Message_FR") %></div></td>
                <td><a href="Modify.aspx?ID=<%#Eval("UID") %>">Modify</a> | <a href="Delete.aspx?ID=<%#Eval("UID") %>">Delete</a></td>
            </tr>
        </ItemTemplate>   
    </asp:Repeater>
</table>

So what's the problem? well, in my repeater, for English and French triggers, i get the System.Collections.Generic.List`1[System.String] instead of the actual list of string... i thought to myself: build a "simple" version of your object that only has a string instead of a list... but then that's a lof of code for just displaying an HTML version of the list.

Is there a way, using nested repeaters, to display the list of strings as separate strings, instead of having to build a different object or using OnItemDataBound event?

Upvotes: 1

Views: 116

Answers (1)

Win
Win

Reputation: 62260

Nested repeaters will work, but it could be overkill. Easiest way is to use string.Join("<br/> ", items) if you do not want to use OnItemDataBound event.

Tip: If you use .NET Framework 4.5 or above, you could use ItemType for strongly-typed data binding so that you won't have to cast object to actual type.

For example,

<asp:Repeater ID="lstTriggers" runat="server" ItemType="AspNetWebForm.TriggerMessage">
    <HeaderTemplate>
        <tr style="border-bottom: 1px solid black;">
            <th>English Triggers</th>
            <th>French Triggers</th>
            <th>English Message</th>
            <th>French Message</th>
            <th>Actions</th>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <div style="overflow-x: scroll; width: 100%">
                    <%# string.Join("<br/>", (List<string>)Eval("Triggers_EN")) %>
                </div>
            </td>
            <td>
                <div style="overflow-x: scroll; width: 100%">
                    <%# string.Join("<br/>", Item.Triggers_FR) %>
                </div>
            </td>
            <td>
                <div style="overflow-x: scroll; width: 100%"><%#Eval("Message_EN") %></div>
            </td>
            <td>
                <div style="overflow-x: scroll; width: 100%"><%#Eval("Message_FR") %></div>
            </td>
            <td><a href="Modify.aspx?ID=<%#Eval("UID") %>">Modify</a> | <a href="Delete.aspx?ID=<%#Eval("UID") %>">Delete</a></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

Upvotes: 2

Related Questions