How do you show x items per row in a repeater?

2020-02-06 08:10发布

Anyone have any clue on how to show 4 items going horizontal with a repeater? A repeater shows items going down by default. Here is my test repeater code so far:

<table border=0 cellpadding=0 cellspacing=0 align="center" width="800px;>
    <tr>
        <asp:Repeater ID="rptTest" runat="server">
            <ItemTemplate>
                    <td>
                        <h3><a href="<%#GetItemLink((Item)Container.DataItem) %>"><%#((WebMenuItem)Container.DataItem).Name %></a></h3>
                        <div>
                            <a href="<%#GetUrl((Item)Container.DataItem) %>">
                                <img src="<%#GetImage((Item)Container.DataItem) %>" alt="<%#GetAltText((Item)Container.DataItem) %>" />
                            </a>
                        </div>
                    </td>
            </ItemTemplate>
        </asp:Repeater>
    </tr>
</table>

标签: asp.net
7条回答
Luminary・发光体
2楼-- · 2020-02-06 08:15

Use a ListView http://msdn.microsoft.com/en-us/library/bb398790.aspx

<asp:ListView runat="server" ID="ListView1" 
    DataSourceID="SqlDataSource1" 
    GroupItemCount="5">
  <LayoutTemplate>
    <table runat="server" id="table1">
      <tr runat="server" id="groupPlaceholder">
      </tr>
    </table>
  </LayoutTemplate>
  <GroupTemplate>
    <tr runat="server" id="tableRow">
      <td runat="server" id="itemPlaceholder" />
    </tr>
  </GroupTemplate>
  <ItemTemplate>
    <td runat="server">
      <%-- Data-bound content. --%>
      <asp:Label ID="NameLabel" runat="server" 
          Text='<%#Eval("Name") %>' />
    </td>
  </ItemTemplate>
</asp:ListView>
查看更多
爷的心禁止访问
3楼-- · 2020-02-06 08:19

Try to set a CSS class for your <td> tags, and set an explicit width.

查看更多
狗以群分
4楼-- · 2020-02-06 08:21

Modifying Nick's suggestion I was able to use to make a 5 horizontal x n vertical Image Grid. Thanks Nick!

<asp:Repeater ID="colorsList" runat="server">
    <HeaderTemplate>
        <table>
        <tr>
    </HeaderTemplate>
    <ItemTemplate>
        <%# (Container.ItemIndex != 0 && Container.ItemIndex % 5 == 0) ? @"</tr><tr>" : string.Empty %>
        <td>
        <div><img src="<%#ColorThumbnailImage((string)DataBinder.Eval(Container.DataItem, "COLOR_SQUARE_IMAGE")) %>" /></div>
        <div><%# DataBinder.Eval(Container.DataItem, "COLOR_NAME") %></div>
        </td>
    </ItemTemplate>
    <FooterTemplate>
        </tr></table>
    </FooterTemplate>
    </asp:Repeater>

And here's ColorThumbnailImage

 protected string ColorThumbnailImage(string fileName)
{
    return Request.ApplicationPath + MySports.System.Configuration.ColorSquareImageLocation + fileName;
}
查看更多
唯我独甜
5楼-- · 2020-02-06 08:26

This might do the job (I'm assuming you may have more than one row of four images):

<asp:Repeater ID="rptTest" runat="server">

<HeaderTemplate>
<table border=0 cellpadding=0 cellspacing=0 align="center" width="800px">
<tr>
</HeaderTemplate>

<ItemTemplate>
<%# (Container.ItemIndex != 0 && Container.ItemIndex % 4 == 0) ? @"</tr><tr>" : string.Empty %>
<td>
<h3><a href="<%#GetItemLink((Item)Container.DataItem) %>"><%# (WebMenuItem)Container.DataItem).Name %></a></h3>
<div>
<a href="<%#GetUrl((Item)Container.DataItem) %>"><img src="<%#GetImage((Item)Container.DataItem) %>" alt="<%#GetAltText((Item)Container.DataItem) %>" /></a>
</div>
</td>
</ItemTemplate>

<FooterTemplate>
</tr>
</table>
</FooterTemplate>

</asp:Repeater>

(no IDE so untested :)

If it doesn't help, some answers from this question might.

查看更多
Melony?
6楼-- · 2020-02-06 08:34

Scott Guthrie provided an example of how to do this with a ListView control in the following article. He uses the control to render an unordered list and uses CSS to control the layout.

查看更多
够拽才男人
7楼-- · 2020-02-06 08:36
<table>
    <asp:Repeater id="rptTest" runat="server">
        <ItemTemplate>
            <%# (Container.ItemIndex + 4) % 4 == 0 ? "<tr>" : string.Empty %>
                <td>
                    ... cell contents omitted ...
                </td>
            <%# (Container.ItemIndex + 4) % 4 == 3 ? "</tr>" : string.Empty %>
        </ItemTemplate>
    </asp:Repeater>
</table>

Long live the humble repeater!

查看更多
登录 后发表回答