Search code examples
htmlasp.netvb.netrepeaterbackground-color

Get divs inside a repeater without using findcontrol


I want to change the background color of a div contained in a repeater. In the code below is the one like: <div id="<%# Eval("id") %>"> My problem is I'm using the divs id to get the reference of a record (the div is the graphical representation of such record), so I can't change it and, as it isn't a fixed control id, I can't use it along with findcontrol in code behind to find the divs. How can I achieve this?

At first I wanted to add Eval in the style attribute of the div, like: style='background-color:<%# Eval("Color")%>;', that here someone suggested as working, but don't work for me.

The color is saved on the db as varchar containing the HEX value.

<asp:Repeater runat="server" ID="rptDaFare" DataSourceID="SqlAttivitaDaFare">
    <ItemTemplate>
        <div id="<%# Eval("id") %>">
            <div class="div-titolo" title="<%# Eval("Titolo") %>"><%# Eval("Titolo") %></div>
            <div class="div-testo" title="<%# Eval("Note") %>"><%# Eval("Note") %></div>
            <div>
                <table style="width: 100%; margin-top: 0.5em; padding-right: 0.2em;">
                    <tr>
                        <td style="width: 50%; text-align: left;">
                            <asp:ImageButton runat="server" ImageUrl="~/images/gabri.png" Width="2.3em" Height="2.3em" ToolTip='<%#Eval("tecnico")%>' Enabled="false" Visible='<%# IIf(Eval("idutente") = 8, True, False) %>' />
                            <asp:ImageButton runat="server" ImageUrl="~/images/giuse.png" Width="2.3em" Height="2.3em" ToolTip='<%#Eval("tecnico")%>' Enabled="false" Visible='<%# IIf(Eval("idutente") = 2, True, False) %>' />
                            <asp:ImageButton runat="server" ImageUrl="~/images/robi.png" Width="2.3em" Height="2.3em" ToolTip='<%#Eval("tecnico")%>' Enabled="false" Visible='<%# IIf(Eval("idutente") = 5, True, False) %>' />
                        </td>
                        <td style="width: 50%; text-align: right;">
                            <asp:LinkButton CommandName="delAttivita" CommandArgument='<%#Eval("ID")%>' runat="server" ID="lnkDelAtt" CausesValidation="False" OnClientClick="return confirm('Sei sicuro di voler eliminare questa attivita?');"><i class="fa fa-trash fa-lg" title="Elimina attività"></i></asp:LinkButton>
                            <asp:LinkButton CommandName="editAttivita" CommandArgument='<%#Eval("ID")%>' runat="server" ID="lnkEditAtt"><i class="fa fa-pencil-square fa-lg" title="Modifica attività"></i></asp:LinkButton>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

Solution

  • If the value of Color is either a correct hex or string representation of a color then it should work. However you did not close the style property correctly.

    If Color is a string representation (red, green, blue)

    <div style="background-color: <%# Eval("Color") %>;">Lorem Ipsum</div>
    

    If the color is HEX without the #

    <div style="background-color: #<%# Eval("Color") %>;">Lorem Ipsum</div>
    

    Or with the #

    <div style="background-color: <%# Eval("Color") %>;">Lorem Ipsum</div>
    

    If one of these does not work then you should check the actual value of Color