1
votes

Good afternoon.

I have a Repeater with a ItemTemplate that prints one column with data.

<asp:Repeater id="OtherProductsRepeater" runat="server">

    <ItemTemplate>
(...data...)
        </ItemTemplate>

</asp:Repeater>

How can i modify the code to instead of one column create three columns to show the data?

Thanks in advance.

3
I take it that the ItemTemplate is a <tr> element with one <td> element containing the data?Russ Cam

3 Answers

5
votes

Edited to show using repeater to make a three column layout

<table>
<asp:Repeater id="OtherProductsRepeater" OnItemCreated="OtherProductsRepeater_ItemCreated" runat="server"> 
  <ItemTemplate> 

    <asp:PlaceHolder id="row_end" visible="<%# _showRowEnd %>" runat="server">
    </tr>
    </asp:PlaceHolder>

    <asp:PlaceHolder id="row_start" visible="<%# _showRowStart %>" runat="server">
    <tr>
    </asp:PlaceHolder>

      <td>(data)</td>
      <td>(data)</td>
      <td>(data)</td>

  </ItemTemplate> 
</asp:Repeater> 

  </tr> <%-- close final row --%>
</table>

in your code, you need these page level members:

private int _rowCounter = 0;
protected bool _showRowEnd;
protected bool _showRowStart;

and the event handler:

protected void OtherProductsRepeater_ItemCreated(Object Sender, RepeaterItemEventArgs e) {
  if (_rowCounter == 0) {  // first row
    _showRowStart = true;
    _showRowEnd = false;
    _rowCounter = 1;
  }
  else if (_rowCounter == 3) {
    _showRowStart = true;
    _showRowEnd = true;
    _rowCounter = 1;
  }
  else {
    _showRowStart = false;
    _showRowEnd = false;
    _rowCounter += 1;
  }
}

Another thought - if you are talking about newspaper-style columns, where the content flows from the bottom of one column to the top of the next, use a DataList control instead of a repeater.

1
votes

be sure to encapsulate all the mark up in the single control in case visible=false, or item.count=0

<asp:Repeater id="OtherProductsRepeater" runat="server"> 
  <HeaderTemplate>
      <table>
  </HeaderTemplate>
  <ItemTemplate> 
    <tr>
      <td>(data)</td>
      <td>(data)</td>
      <td>(data)</td>
    </tr>
  </ItemTemplate> 
  <FooterTemplate>
      </table>
  </FooterTemplate>
</asp:Repeater> 
0
votes

If you're using ASP.net 3.5 or higher you should use ListView and the GroupTemplate.

For example code, See...

http://www.4guysfromrolla.com/articles/010208-1.aspx