repeater control - change the background color depending on a value

B

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
 
P

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>
 
B

bill

Thank you very much!

Phillip Williams said:
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">

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top