asp.netvb.netrepeaterfindcontrol

How to use findControl to get divs inside a repeater in asp.net


I have a inside a repeater, where its ID is obtained from a record in a data source using <div id="<%# Eval("id") %>">. What I'm attempting to accomplish is changing the background color of some of these divs based on specific data retrieved through a query. Despite trying various approaches, I'm unable to access these divs as they always return null. I've attached the ASPX and current code-behind files for reference.

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!


Solution

  • 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") %>'