If statement in repeaters ItemTemplate
Solution 1
I would use codebehind:
protected void OnCheckboxListItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlTableRow itemRow = (HtmlTableRow) e.Item.FindControl("itemRow");
itemRow.Visible = e.Item.ItemIndex % 2 == 0;
}
}
Solution 2
Another way of doing this (if performance is not a problem):
<ItemTemplate>
<!-- "If" -->
<asp:PlaceHolder runat="server" Visible="<%# MyCondition %>">
<tr><td></td></tr>
</asp:PlaceHolder>
<!-- "Else" -->
<asp:PlaceHolder runat="server" Visible="<%# !MyCondition %>">
<tr><td></td></tr>
</asp:PlaceHolder>
</ItemTemplate>
Solution 3
If you're trying yo make a 2 columns table this could do the trick
<%# Container.ItemIndex % 2 == 0 ? "<tr class='itemRow'>" : "" %>
<td>
Some data
</td>
<%# Container.ItemIndex % 2 != 0 ? "</tr> : "" %>
Changed a couple of things: id="itemRow" for all rows would cause repeated ids what is not allowed.
Removed runat="server" since doesn't make sense on this context.
Solution 4
I have 2 examples, for the examples i will bind the repeater to a array of strings (demonstration purposes only)
void BindCheckboxList()
{
checkboxList.DataSource = new string[] { "RowA", "RowB", "RowC", "RowD", "RowE", "RowF", "RowG" };
checkboxList.DataBind();
}
Example 1: Create a methode in de codebehind casting the bound elements back en evaluate what ever value you'd like.
Create Methode in CodeBehind (example 1):
protected string StringDataEndsWith(object dataElement, string endsWith, string returnValue)
{
// for now an object of the type string, can be anything.
string elem = dataElement as string;
if (elem.EndsWith(endsWith))
{
return returnValue;
}
else
{
return "";
}
}
In the .aspx file (example 1):
<asp:Repeater ID="checkboxList" runat="server">
<HeaderTemplate>
<table style="padding:0px;margin:0px;">
</HeaderTemplate>
<ItemTemplate>
<%# StringDataEndsWith(Container.DataItem,"A","<tr id=\"itemRow\" runat=\"server\">") %>
<td>
<%# Container.DataItem %>
</td>
<%# StringDataEndsWith(Container.DataItem,"G","</tr>") %>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Example 2: You could use a direct cast in the .aspx file
DirectCast example (no code behind):
<asp:Repeater ID="checkboxList" runat="server">
<HeaderTemplate>
<table style="padding:0px;margin:0px;">
</HeaderTemplate>
<ItemTemplate>
<%# Convert.ToString(Container.DataItem).EndsWith("A") ? "<tr id=\"itemRow\" runat=\"server\">" : "" %>
<td>
<%# Container.DataItem %>
</td>
<%# Convert.ToString(Container.DataItem).EndsWith("G") ? "</tr>" : "" %>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I hope this is what you're looking for. Regards.
Vivendi
Updated on July 09, 2022Comments
-
Vivendi 11 monthsI'm using an ASP.NET
Repeaterto display the contents of a<table>. It looks something like this:<table cellpadding="0" cellspacing="0"> <asp:Repeater ID="checkboxList" runat="server" OnItemDataBound="OnCheckboxListItemBound"> <ItemTemplate> <tr id="itemRow" runat="server"> <td> Some data </td> </tr> </ItemTemplate> </asp:Repeater> </table>It works fine, but i'd like to have an
if()statement inside theItemTemplateso i can conditionally determine if i want to print out a<tr>tag.So i'd like to have something like this:
<table cellpadding="0" cellspacing="0"> <asp:Repeater ID="checkboxList" runat="server" OnItemDataBound="OnCheckboxListItemBound"> <ItemTemplate> <% if ( (CurrentItemCount % 2) == 0 ) { %?> <tr id="itemRow" runat="server"> <% } %> <td> Some data </td> <% if ( (CurrentItemCount % 2) == 0 ) { %?> </tr> <% } %> </ItemTemplate> </asp:Repeater> </table>Is there some way i can achieve this?
PS. The
CurrentItemCountis just made up. I also need a way to get the current item count inside thatif()statement. But i only seem to be able to get it from<%# Container.ItemIndex; %>, which can't be used with anif()statement?