asp.net repeater, output over 2 table columns

DaFou
DaFou used Ask the Experts™
on
Hi All,

I want to use a reapter control to display items from my datasource.
I'd like to display them over 2 columns in an html table.
How do I output html in a repeater over 2 columns even when the rowcount is odd?

So this should be possible:
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
  <tr>
    <td>item3</td>
    <td>!!no more items</td>
  </tr>
</table>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
1) Put the <table> element in the header
2) Put the <tr> and <td> elements in the ItemTemplate and AlternatingItemTemplate
3) Put the </table> tag in the footer

Bob

Author

Commented:
ill try but why would the alternating item begin with a <tr>?

Author

Commented:
i tried, but the output makes no valid html :-( perhaps i am missing your finer point?
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Most Valuable Expert 2012
Top Expert 2008

Commented:
1) You use the AlternatingItemTemplate for different colors or styles for elements.

2) If you don't need that, than just use the ItemTemplate.

3) What does the ItemTemplate look like?

4) What does the resulting invalid HTML look like?

Bob

Author

Commented:
I want my data source spread out over a 2 column table.
so if only 1 row present then:
<table>
  <tr>
    <td>item1></td>
    <td>!!nomore items!!</td>
  </tr>
</table>

if 2 rows then:
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
</table>

if 3 items then:
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
  <tr>
    <td>item3</td>
    <td>!!no more items</td>
  </tr>
</table>
if 4 items then :
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
  <tr>
    <td>item3</td>
    <td>item 4</td>
  </tr>
</table>
if 5 items then :
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
  <tr>
    <td>item3</td>
    <td>item4</td>
  </tr>
  <tr>
    <td>item5></td>
    </td>!!no more items</td>
  </tr>
</table>

etc etc

how to do this?


Most Valuable Expert 2012
Top Expert 2008

Commented:
How did you define your template?

Bob

Author

Commented:
I have no idea, can you help me define a template?
the reason i have not defined a template is because i cannot figure out how to output valid html as a result of the repeater in my scenario

Author

Commented:
i am looking for the most basic of all tempaltes in relation to a 2 column table.
I am begining to think this can only be done via custom control, just like a grouped repeater is also not available using the default repeater

Author

Commented:
Bob,

in asnwer to your questin how the invalid HTML looks like using your suggestion:
1) Put the <table> element in the header
2) Put the <tr> and <td> elements in the ItemTemplate and AlternatingItemTemplate
3) Put the </table> tag in the footer

a 1 row datasource



<table class="g_1024">
    <tr>
      <td class="g_1024_4c_1">a
</table>

How can a repeater be used over a 2 column table?
Most Valuable Expert 2012
Top Expert 2008

Commented:
Try something like this example:

<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>
   <table border="0">
</HeaderTemplate>
<ItemTemplate>
  <tr>
     <td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
     <td><%# DataBinder.Eval(Container.DataItem, "URL") %></td>
  </tr>
</ItemTemplate>
<FooterTemplate>
  </table>
</FooterTemplate>
</asp:Repeater>

Bob

Author

Commented:
Bob,

your last sugestion is a regular row by row repeater in where there are 2 fields to be extracted on each row.
My scenario is about we dont know how many rows there are and we want to output only one field from every row spread out over a 2 column table.

I want my data source spread out over a 2 column table.
so if only 1 row present then:
<table>
  <tr>
    <td>item1></td>
    <td>!!nomore items!!</td>
  </tr>
</table>

if 2 rows then:
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
</table>

if 3 items then:
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
  <tr>
    <td>item3</td>
    <td>!!no more items</td>
  </tr>
</table>
if 4 items then :
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
  <tr>
    <td>item3</td>
    <td>item 4</td>
  </tr>
</table>
if 5 items then :
<table>
  <tr>
    <td>item1></td>
    <td>item2</td>
  </tr>
  <tr>
    <td>item3</td>
    <td>item4</td>
  </tr>
  <tr>
    <td>item5></td>
    </td>!!no more items</td>
  </tr>
</table>

etc etc

how to do this?
Most Valuable Expert 2012
Top Expert 2008

Commented:
Are you asking for a way to take one column of data from a DataTable, and display that across multiple columns in an HTML table?

Bob

Author

Commented:
no, i have a reapter. the datasource has only one field but has X rows.
I want to list that one and only field of every row spread out in a 2 column table

I want my data source spread out over a 2 column table.
so if only 1 row present then:
<table>
  <tr>
    <td>only field of first row</td>
    <td>!!nomore rows, thus also no more field like the previous row!!</td>
  </tr>
</table>

if 2 rows then:
<table>
  <tr>
    <td>only field of first row</td>
    <td>only field of second row</td>
  </tr>
</table>

if 3 items then:
<table>
  <tr>
    <td>only field of first row</td>
    <td>only field of second row</td>
  </tr>
  <tr>
    <td>only field of third row</td>
    <td>!!nomore rows, thus also no more field like the previous row!!</td>
  </tr>
</table>
etc etc

how to do this?

Author

Commented:
if 3 items then:
<table>
=
if 3 rows then:
<table>

Author

Commented:
I figure that one needs to customize the repeater control, more specifically handling the ItemCreated event. The only way to know if there are more items present then the current is when using a bidirectional ( or some other type that knows its row size ) datasource.

I recommend closing this question with a point refund while leaving it available for others that might run into the same scenario in the future.

regards
Top Expert 2007
Commented:
a possible solution would be to use a datalist.
set the datalist to 2 columns.
then before you bind the datalist, you can count the rows.
use modulus 2 to figure out if it is an odd number of rows, and if it is, add one row to the datatable then bind to the datalist
...i'm thinking that would give you the results you're trying to achieve.

Author

Commented:
although in concept no different then using a repeater I did find complementing the rowcount to an even number an interesting idea. However I am not sure it makes any diference how to handle that empty odd column wether datalist or repeater.

As much as would have liked to grant the learnedone at least some points I found no justification in this thread to that end. sorry about that bob :-(

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial