asp.net如何使用findControl在转发器中获取div

问题描述 投票:1回答:2

我在转发器中有一个div,它的id是来自数据源<div id="<%# Eval("id") %>">记录的id我想要做的是改变一些div的背景颜色(通过查找特定数据的查询),I尝试了不同的方法,但我不能得到div,它们总是空的。我附上了aspx和当前代码。

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>

代码背后

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

我怎样才能做到这一点?谢谢!

asp.net vb.net repeater findcontrol
2个回答
1
投票

我建议在aspx端做一个更简单的解决方案。另外,我总是建议使用css-classes进行格式化。

这是一个例子:

CSS:

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

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

中继器:

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

1
投票

好的,多亏了这里的一些提示和一些谷歌搜索,我使用这个适用于我的解决方案:

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") %>' 
© www.soinside.com 2019 - 2024. All rights reserved.