ASP.Net Relay Control
3 answers
Try something like this in your code
protected void rpt_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem != null)
{
string color = (string)DataBinder.Eval(e.Item.DataItem, "RowColor");
HtmlTableRow rowToColor = (HtmlTableRow)e.Item.FindControl("Row");
rowToColor.Attributes.CssStyle.Add("background-color", color );
}
}
and something like this in the aspx page
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_OnItemDataBound">
<ItemTemplate>
<tr id="Row" runat="server">
<td>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
+4
a source to share
Use the binding syntax <% #%> in the ItemTemplate for conditional formatting:
<asp:Repeater runat="server" ID="rpt">
<ItemTemplate>
<div class="<%# Container.ItemIndex % 2 ? "even" : "odd" %>">
</div>
<div class="<%# Eval("PropertyOfDataSource") %>">
</div>
</ItemTemplate>
</asp:Repeater>
+5
a source to share
You can solve this in different ways, and depending on what your repeater should look like or the data you are sending, there is no better answer. Here is a workaround that will evaluate the data and compare it for the correct color response.
In the element template, the repeater is surrounded by a div.
<div style="background-color:<%# GetBG((string)(DataBinder.Eval(Container.DataItem,"DataField")))%>">
and in the code behind there is a function to solve
public string GetBG(string demo)
{
if (demo == "TestData2")
return "yellow";
return "Green";
}
This is not a good way to do it and is rather wasteful. The onDataBind function would be the best way, just wanted to show another way to accomplish this task.
0
a source to share