If statement in repeaters ItemTemplate
Asked Answered
V

5

10

I'm using an ASP.NET Repeater to 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 the ItemTemplate so 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 CurrentItemCount is just made up. I also need a way to get the current item count inside that if() statement. But i only seem to be able to get it from <%# Container.ItemIndex; %>, which can't be used with an if() statement?

Votaw answered 18/6, 2013 at 12:27 Comment(2)
Is there a reason why you can't use a gridview to display tabular data ?Psychiatrist
@Bartdude Yes, i'm adjusting existing code and i really don't want to rewrite alot of functionality. So if it's possible somehow with my code then i'd really like to stick to that.Votaw
S
-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;
    }
}
Slumber answered 18/6, 2013 at 12:32 Comment(8)
Since i needed to keep the ID attribute of the row, this was the best solution for me.Votaw
This answer does not show how to use an if statement inside an ItemTemplate. It should really just be a comment.Agonistic
@edward: but it shows the best way if you want to use an if statement and you use an item template. Best in terms of fail safety in general and also compile time safety. It is also the more readable and maintainable way. Actually even OP has preferred this. I woulnt use ASP.NET if i still want to code classic ASP or PHP. Apart from that i cannot put this all into a comment.Slumber
It's arguably less readable and maintainable. The standard approach of ASP.NET is to have UI logic in mark up and business logic in the C# file. Putting UI logic into the C# file is almost as bad as ASP and PHP are putting business logic code into the mark up. Here's one of the examples I've found that actually does that correctly: https://mcmap.net/q/1160550/-datalist-conditional-statements-in-lt-itemtemplate-gtAgonistic
The aspx is for controls and stylesheets(layout) only. Code or logic belongs to codebehind. Imho inline code is for testing or demonstration purposes only apart from Eval/Bind or javascript.Slumber
Yes, except UI code or logic does not belong into codebehind.Agonistic
UI logic is business logic. No code belongs to the UI. You cannot test,compile,maintain,reuse code which is mixed with UI, no intellisense etc, etc.Slumber
@TimSchmelter UI logic is not business logic. "In computer software, business logic or domain logic is the part of the program that encodes the real-world business rules that determine how data can be created, displayed, stored, and changed. It is contrasted with the remainder of the software which might be concerned with lower-level details of managing a database or displaying the user interface, system infrastructure, or generally connecting various parts of the program." en.wikipedia.org/wiki/Business_logicAgonistic
O
23

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>
Opening answered 5/5, 2015 at 9:23 Comment(4)
Well, very much depending on your scenario of course. My experience is that in a normal use case you will end up performing the same instructions within all placeholders multiple times, one for each condition. The instructions within the hidden placeholder will still be executed, even though no markup gets sent to the client.Opening
when we set Visible to false the markup will not sent to client.Fairchild
Yes, but I think that in cases where the visibility condition is not static (determined upon databinding), all logic defined in both placeholders will be evaluated/executed on the server. Possibly causing a performance drop but not necessarily.Opening
I used this one with no performance problem. My repeater only gets a 100 to 200 rows. Perfect solution for the conditional content.Maisel
V
16

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.

Vahe answered 18/6, 2013 at 12:32 Comment(0)
K
1

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.

Kame answered 18/6, 2013 at 13:23 Comment(0)
O
0

If you're wanting to do something on every other item, use the alternating item template. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.alternatingitemtemplate.aspx

Ogren answered 17/10, 2013 at 16:6 Comment(0)
S
-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;
    }
}
Slumber answered 18/6, 2013 at 12:32 Comment(8)
Since i needed to keep the ID attribute of the row, this was the best solution for me.Votaw
This answer does not show how to use an if statement inside an ItemTemplate. It should really just be a comment.Agonistic
@edward: but it shows the best way if you want to use an if statement and you use an item template. Best in terms of fail safety in general and also compile time safety. It is also the more readable and maintainable way. Actually even OP has preferred this. I woulnt use ASP.NET if i still want to code classic ASP or PHP. Apart from that i cannot put this all into a comment.Slumber
It's arguably less readable and maintainable. The standard approach of ASP.NET is to have UI logic in mark up and business logic in the C# file. Putting UI logic into the C# file is almost as bad as ASP and PHP are putting business logic code into the mark up. Here's one of the examples I've found that actually does that correctly: https://mcmap.net/q/1160550/-datalist-conditional-statements-in-lt-itemtemplate-gtAgonistic
The aspx is for controls and stylesheets(layout) only. Code or logic belongs to codebehind. Imho inline code is for testing or demonstration purposes only apart from Eval/Bind or javascript.Slumber
Yes, except UI code or logic does not belong into codebehind.Agonistic
UI logic is business logic. No code belongs to the UI. You cannot test,compile,maintain,reuse code which is mixed with UI, no intellisense etc, etc.Slumber
@TimSchmelter UI logic is not business logic. "In computer software, business logic or domain logic is the part of the program that encodes the real-world business rules that determine how data can be created, displayed, stored, and changed. It is contrasted with the remainder of the software which might be concerned with lower-level details of managing a database or displaying the user interface, system infrastructure, or generally connecting various parts of the program." en.wikipedia.org/wiki/Business_logicAgonistic

© 2022 - 2024 — McMap. All rights reserved.