How to write asp based HTML dynamically: Repeater value

I am using asp: Repeater control on my .aspx page, which is similar to:

<ol>
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <li>                   
                <%# Container.DataItem %>
            </li>
        </ItemTemplate>
    </asp:Repeater>
</ol>

Note. In the combination lock, I associate a common data list with the Repeater1 control

I'm struggling to figure out how I can trap the values ​​of Container.DataItem, and then, depending on the value, change the tag style attribute [li style = "myStyle"].

I am looking for an inline solution, so the pseudocode will look something like this:

<ol>
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
           <% if(Container.DataItem == "some value")
            {
            <li style="style1">                   
                <%# Container.DataItem %>
            </li>
            }
            else
            {
             <li style="style2">                   
                <%# Container.DataItem %>
            </li>
            }
            %>
        </ItemTemplate>
    </asp:Repeater>
</ol>

Is there a built-in way to execute the above psuedo code example? If so, how?

+3
source share
3 answers

You can try:

<ol>
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <li style="<%# (string) Container.DataItem == "some value" ? "style1" : "style2" %>">                   
                <%# Container.DataItem %>
            </li>
        </ItemTemplate>
    </asp:Repeater>
</ol>
+2
source
<ol>
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
           <%# string value = Container.DataItem as string; %>
            <li class="<%=value == "some value" ? "style1" : "style2" %>">
                <%=value %>
            </li>
        </ItemTemplate>
    </asp:Repeater>
</ol>
+2
source

.

However, you must use Container.DataItemfor the string. Otherwise, you will get a comparative comparison.
If you are not attached to the list of strings, you can use Container.DataItemfor any type that is actually there and do something with it.

0
source

Source: https://habr.com/ru/post/1780037/


All Articles