0

I have a div inside a repeater, its id is the id get from a record from a datasource <div id="<%# Eval("id") %>"> What I'm trying to do is changing the background color of some of this divs (through a query that looks for specific data), I tried different approaches but I can't get the divs, they are always null. I attach aspx and current code behind.

ASPX

<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>

CODE BEHIND

Private Sub rptDaFare_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rptDaFare.ItemDataBound
        If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
            Dim data As Date = Date.ParseExact(txtData.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
            Dim attivita = dbVulcano.Attivita.Where(Function(a) a.Completato = False And (a.IDUtente = -1 Or a.IDUtente = ddlTec.SelectedValue) And (a.DataInizio.HasValue = False Or a.DataInizio <> data) And (a.inCorso = False Or a.inCorso.HasValue = False))
            For Each att In attivita
                Dim div As HtmlGenericControl = TryCast(e.Item.FindControl(att.ID), HtmlGenericControl)
                If att.DataInizio <= Date.Today Then
                    div.Style("background-color") = "red"
                Else
                    div.Style("background-color") = "antiquewhite"
                End If
            Next
        End If

    End Sub

How can I do this? Thank you!

  • 1
    if I remember right - FindControl finds server side controls. Your divs in question aren't serverside but only client side. I'm however not sure I'd do it in this manner, but you could try putting runat=server on your divs to get them to be server side as well. – Allan S. Hansen Jan 05 '18 at 10:55
  • Allan is correct, you can add runat="server" to this div and then find it on server side. However would it be much easier to just add a css-class (based on the Container.DataItem.DataInizio) to the div inside the repeater when the div is created? – Esko Jan 05 '18 at 10:58
  • if i set runat="server" then I cannot use id="<%# Eval("id") %>" anymore, it returns a parser error – Gabriele Cozzolino Jan 05 '18 at 12:30
  • @Esko can you be more specific? Basically I need to turn red the divs of expired activities, so I have to check if datainizio < today. The databound occour directly from aspx, there isn't a databind code behind, so the above logic should be done in the aspx. How? – Gabriele Cozzolino Jan 05 '18 at 12:43

2 Answers2

1

I would suggest an easier solution by doing this on the aspx-side. Also I always suggest to use css-classes for formatting.

Here is an example:

Css:

<style>
    .task {
        background-color: #faebd7;
    }

    .task.task--past {
        background-color: #ff0000;
    }
</style>

Repeater:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div class="<%#If(CType(Container.DataItem, (Add your namespace here).MyClass).DataInizio <= Date.Today, "task task--past", "task") %>">
            This is content
        </div>
    </ItemTemplate>
</asp:Repeater> 
Esko
  • 4,109
  • 2
  • 22
  • 37
0

Ok, thanks to some hint here and some googling, I used this solution that works for me:

class='<%# IIf(Eval("datainizio").ToString() IsNot DBNull.Value, IIf(String.Format("{0:dd/MM/yyyy}", Eval("datainizio")) <= String.Format("{0:dd/MM/yyyy}", Date.Today), "coloreAttivitaScad", "coloreAttivitaOk"), "coloreAttivitaOk") %>'