views:

329

answers:

3

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  A: 

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.

Ray
My goal is to show the data from the left to the right in 3 columns, with n rows. Is there any possibility to do this using the repeater?
Filipe Costa
If I understand what you want to do, a DataList will do this easily. With a repeater, you can proably do it, but you will need conditional display of <tr> and <td> elements based on record counts (for example, on every third record, display</tr><tr> to start a new row).
Ray
Using the ItemTemplate, AlternatingItemTemplate and the <tr> and <td> is it possible to do it like i want, with the three columns?I can only create a line with two columns using them, without repeating data. :(
Filipe Costa
I don't think so, but I don't really know what you want to do in detail. As you point out, using ItemTemplate and AlternatingItemTemplate only gets you 2 sections. I suggest using PlaceHolder controls to contain your <tr>, </tr>, <td>, and </td> elements. Count your columns in your repeater ItemCreated event and show or hide the row and cell elements as needed to get the layout you want. If this doesn't work for you, then please post some details on the data you have and a picture of the output you want.
Ray
Well, you can see part of the code and the output that i want here:http://imgur.com/DAEO0.png
Filipe Costa
I edited my reply to show my suggestion. It's a little ugly, but it works. I also still suggest checking out the DataList control.
Ray
Thank you Ray. Working now. :)
Filipe Costa
+1  A: 

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> 
Glennular
A: 

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

kervin