Home All Groups Group Topic Archive Search About

repeater control - change the background color depending on a value

Author
20 Mar 2006 5:02 PM
bill
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

Author
20 Mar 2006 5:41 PM
Phillip Williams
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>

Show quoteHide quote
"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
>
>
>
Author
20 Mar 2006 8:00 PM
bill
Thank you very much!

Show quoteHide quote
"Phillip Williams" <WEBSWAPP@newsgroups.nospam> wrote in message
news:9D06EE5C-CD61-4EEC-AD95-5B08F92AF9BD@microsoft.com...
> 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
>>
>>
>>