repeater control - change the background color depending on a value

Discussion in 'ASP .Net Web Controls' started by bill, Mar 20, 2006.

  1. bill

    bill Guest

    How can I change the background color of a repeater item based on the value
    of a control?

    I saw the helpful post from Philip Williams, and I can change the appearance
    of a single control, but I need the entire item to have a different
    background color.

    Thanks!
    Bill


    Protected Sub repeater1_ItemDataBound(ByVal sender As System.Object, ByVal e
    As RepeaterItemEventArgs) Handles repeater1.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    ListItemType.AlternatingItem Then
    'this line gets you the underlying data row view (if you used a
    datatable as
    'the datasource for the repeater).
    Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
    'assuming that you gave the hyperlink an id="HyperLink1"
    Dim hlHyperlink1 As HyperLink =
    CType(e.Item.FindControl("HyperLink1"), HyperLink)
    'assuming you have a field named Text in your dataTable
    hlHyperlink1.Text = drv("Text")
    'assuming you have a field named Url in your datatable
    hlHyperlink1.NavigateUrl = drv("Url")
    End If
    End Sub
     
    bill, Mar 20, 2006
    #1
    1. Advertising

  2. RE: repeater control - change the background color depending on a valu

    Since the repeater item does not render a specific HTML object, you need to
    style the object that you placed within the ItemTemplate. For example, if
    the ItemTemplate contains an object of type HTMLTable then either:

    1) declaratively use the class attribute and a databinding expression,

    2) in the code behind (as the snippet of code that you posted below did)
    find the HTMLTable (which you would have to define as runat=server and give
    it an ID) and then change its class attribute value.


    <asp:Repeater Runat="server" ID="repeater1">
    <ItemTemplate>
    <table class='<%#IIF(DataBinder.Eval(Container.DataItem,"fieldName") >
    0,"oneStyle","anotherStyle")%>' runat="server" id="tbl1">
    <TR>
    <TD>
    <%--Implementation of the content --%>
    </TD>
    </TR>
    </table>
    </ItemTemplate>
    </asp:Repeater>

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "bill" wrote:

    > How can I change the background color of a repeater item based on the value
    > of a control?
    >
    > I saw the helpful post from Philip Williams, and I can change the appearance
    > of a single control, but I need the entire item to have a different
    > background color.
    >
    > Thanks!
    > Bill
    >
    >
    > Protected Sub repeater1_ItemDataBound(ByVal sender As System.Object, ByVal e
    > As RepeaterItemEventArgs) Handles repeater1.ItemDataBound
    > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    > ListItemType.AlternatingItem Then
    > 'this line gets you the underlying data row view (if you used a
    > datatable as
    > 'the datasource for the repeater).
    > Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
    > 'assuming that you gave the hyperlink an id="HyperLink1"
    > Dim hlHyperlink1 As HyperLink =
    > CType(e.Item.FindControl("HyperLink1"), HyperLink)
    > 'assuming you have a field named Text in your dataTable
    > hlHyperlink1.Text = drv("Text")
    > 'assuming you have a field named Url in your datatable
    > hlHyperlink1.NavigateUrl = drv("Url")
    > End If
    > End Sub
    >
    >
    >
     
    Phillip Williams, Mar 20, 2006
    #2
    1. Advertising

  3. bill

    bill Guest

    Re: repeater control - change the background color depending on a valu

    Thank you very much!

    "Phillip Williams" <> wrote in message
    news:...
    > Since the repeater item does not render a specific HTML object, you need
    > to
    > style the object that you placed within the ItemTemplate. For example, if
    > the ItemTemplate contains an object of type HTMLTable then either:
    >
    > 1) declaratively use the class attribute and a databinding expression,
    >
    > 2) in the code behind (as the snippet of code that you posted below did)
    > find the HTMLTable (which you would have to define as runat=server and
    > give
    > it an ID) and then change its class attribute value.
    >
    >
    > <asp:Repeater Runat="server" ID="repeater1">
    > <ItemTemplate>
    > <table class='<%#IIF(DataBinder.Eval(Container.DataItem,"fieldName")
    > >

    > 0,"oneStyle","anotherStyle")%>' runat="server" id="tbl1">
    > <TR>
    > <TD>
    > <%--Implementation of the content --%>
    > </TD>
    > </TR>
    > </table>
    > </ItemTemplate>
    > </asp:Repeater>
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "bill" wrote:
    >
    >> How can I change the background color of a repeater item based on the
    >> value
    >> of a control?
    >>
    >> I saw the helpful post from Philip Williams, and I can change the
    >> appearance
    >> of a single control, but I need the entire item to have a different
    >> background color.
    >>
    >> Thanks!
    >> Bill
    >>
    >>
    >> Protected Sub repeater1_ItemDataBound(ByVal sender As System.Object,
    >> ByVal e
    >> As RepeaterItemEventArgs) Handles repeater1.ItemDataBound
    >> If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    >> ListItemType.AlternatingItem Then
    >> 'this line gets you the underlying data row view (if you used
    >> a
    >> datatable as
    >> 'the datasource for the repeater).
    >> Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
    >> 'assuming that you gave the hyperlink an id="HyperLink1"
    >> Dim hlHyperlink1 As HyperLink =
    >> CType(e.Item.FindControl("HyperLink1"), HyperLink)
    >> 'assuming you have a field named Text in your dataTable
    >> hlHyperlink1.Text = drv("Text")
    >> 'assuming you have a field named Url in your datatable
    >> hlHyperlink1.NavigateUrl = drv("Url")
    >> End If
    >> End Sub
    >>
    >>
    >>
     
    bill, Mar 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrwoopey
    Replies:
    1
    Views:
    2,595
    mrwoopey
    Apr 29, 2004
  2. Volkan Karaboða

    changing row color depending on a cell value

    Volkan Karaboða, Feb 20, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    192
    Volkan Karaboga
    Feb 23, 2004
  3. James Radke

    Set repeater row background color based on data value - how?

    James Radke, Jan 20, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    1,624
    Jeffrey Tan[MSFT]
    Jan 21, 2004
  4. fig000
    Replies:
    0
    Views:
    4,557
    fig000
    Sep 6, 2004
  5. Replies:
    1
    Views:
    274
    marss
    Feb 14, 2007
Loading...

Share This Page