ASP.Net Relay Control

I'm trying all I know to change the line color in asp: repeater. I am trying to do the following, based on the value, I want to be able to set the color of the entry in the relay control. I've tried DIV tags, can't get it to work. How can I do it? Thaks

+1


a source to share


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>
            &nbsp;
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

      

+4


a source


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


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







All Articles