asp.net 如何使用 findControl 获取中继器内的 div
asp.net how to use findControl to get div inside a repeater
我在转发器中有一个 div,它的 ID 是从数据源 <div id="<%# Eval("id") %>">
的记录中获取的 ID
我想做的是改变其中一些 divs 的背景颜色(通过查找特定数据的查询),我尝试了不同的方法但我无法获得 divs ,它们始终为空。我在后面附上 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
我该怎么做?谢谢!
我建议通过在 aspx 端执行此操作来提供更简单的解决方案。此外,我总是建议使用 css-类 进行格式化。
这是一个例子:
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>
好的,多亏了这里的一些提示和一些谷歌搜索,我使用了这个适合我的解决方案:
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") %>'
我在转发器中有一个 div,它的 ID 是从数据源 <div id="<%# Eval("id") %>">
的记录中获取的 ID
我想做的是改变其中一些 divs 的背景颜色(通过查找特定数据的查询),我尝试了不同的方法但我无法获得 divs ,它们始终为空。我在后面附上 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
我该怎么做?谢谢!
我建议通过在 aspx 端执行此操作来提供更简单的解决方案。此外,我总是建议使用 css-类 进行格式化。
这是一个例子:
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>
好的,多亏了这里的一些提示和一些谷歌搜索,我使用了这个适合我的解决方案:
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") %>'