I would like to be able to use the ASP.Net Repeater control to create an HTML Table that has three columns and as many rows as necc.
For example if the Data were to look like this
"Phil Hughes"
"Andy Petite"
"CC Sabathia"
"AJ Burnett"
"Javier Vazquez"
I would like the resulting table to be like
<table>
<tr>
<td>Phil Hughes</td>
<td>Andy Petite</td>
<td>CC Sabathia</td>
</tr>
<tr>
<td>AJ Burnett</td>
<td>Javier Vazquez</td>
<td></td>
</tr>
</table>
How can I do this?
It's better to use a DataList control intstead as it has the interesting properties RepeatColumns and RepeatDirection.
Or just use a div in the repeater and then solve the hight/width issues with CSS.
Repeater is not the ideal control to do that. If you're using .NET 3.5 you should use ListView instead. Here's an example that does what you're asking for.
Much simpler than all the examples listed here; You don't need to use a list view or do anything in the code behind.
I am assuming you have all those name in 5 rows of data and you want to spread it across 3 columns in a repeater and not have 2 rows of data with 3 fields which would be straight forward. Based on my assumption your data is something like:
DataTable
(or whatever your source is):You can do it using a
Repeater
and aLiteral
with a little logic on theOnDataBinding
event of theLiteral
.First define your
Repeater
:Next you will need a constant for the total columns you want and two global variables to track the binding operation. Define them like so:
Then you will need to implement the OnDataBinding to do all the custom work:
Then make sure when you bind your
Repeater
you are saving the total count:The output produced would be:
You could probably improve the
DataBinding
code but I just rattled it off to give the basic premise of how to accomplish your goal. If theDataBinding
needs to do a lot of string concat operations, you should probably switch to to using aStringBuilder
and then just assign theLiteral
in the last operation.