How to change the row color of the Repeater based on some condition?

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

I have a repeater web control. Currently I want to change some row's color
based on defined condition. Is there any code sample demonstrating how to
accomplish it?

Thanks.
 
Hi Michael,

As Munna has suggested, for repeater control ,if the conditional code logic
is complex (and not convenient to implement via inline databinding
expression), you can consider using ItemDataBound event. Here is a simple
sample which use repeater to display multiple html table. And I use
"ItemDatdaBound" event to set the background color of each table:


==========aspx page====================
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">

<ItemTemplate>
<table id="tb" runat="server">
<tr>
<td>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" />
</td>
</tr>
</table>
</ItemTemplate>

</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
SelectCommand="SELECT [id], [name] FROM
[numtable]"></asp:SqlDataSource>
</form>
==============code behind===============

Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
Repeater1.ItemDataBound
Dim tb As HtmlTable = e.Item.FindControl("tb")

If e.Item.ItemIndex Mod 2 = 0 Then
tb.Style(HtmlTextWriterStyle.BackgroundColor) = "yellow"
End If


End Sub
=================================

Here are some other web articles mentioned some samples of using
ItemDataBound event to customize repeater or datalist control:

#ASP.NET Tip: Use the ItemDataBound Event of a Repeater
http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/

#Formatting the DataList and Repeater Based Upon Data
http://www.asp.net/Learn/Data-Access/tutorial-30-vb.aspx


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Thanks very much for both of you. It is clear now.

Steven Cheng said:
Hi Michael,

As Munna has suggested, for repeater control ,if the conditional code
logic
is complex (and not convenient to implement via inline databinding
expression), you can consider using ItemDataBound event. Here is a simple
sample which use repeater to display multiple html table. And I use
"ItemDatdaBound" event to set the background color of each table:


==========aspx page====================
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">

<ItemTemplate>
<table id="tb" runat="server">
<tr>
<td>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" />
</td>
</tr>
</table>
</ItemTemplate>

</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
SelectCommand="SELECT [id], [name] FROM
[numtable]"></asp:SqlDataSource>
</form>
==============code behind===============

Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
Repeater1.ItemDataBound
Dim tb As HtmlTable = e.Item.FindControl("tb")

If e.Item.ItemIndex Mod 2 = 0 Then
tb.Style(HtmlTextWriterStyle.BackgroundColor) = "yellow"
End If


End Sub
=================================

Here are some other web articles mentioned some samples of using
ItemDataBound event to customize repeater or datalist control:

#ASP.NET Tip: Use the ItemDataBound Event of a Repeater
http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/

#Formatting the DataList and Repeater Based Upon Data
http://www.asp.net/Learn/Data-Access/tutorial-30-vb.aspx


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
Reply-To: "Michael" <[email protected]>
From: "Michael" <[email protected]>
Subject: How to change the row color of the Repeater based on some condition?
Date: Thu, 19 Jun 2008 14:34:13 +0800
I have a repeater web control. Currently I want to change some row's color
based on defined condition. Is there any code sample demonstrating how to
accomplish it?

Thanks.
 
You're welcome Michael,

If there is anything else we can help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Reply-To: "Michael" <[email protected]>
From: "Michael" <[email protected]>
References: <[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: How to change the row color of the Repeater based on some condition?
Date: Fri, 20 Jun 2008 11:26:47 +0800
Thanks very much for both of you. It is clear now.

Steven Cheng said:
Hi Michael,

As Munna has suggested, for repeater control ,if the conditional code
logic
is complex (and not convenient to implement via inline databinding
expression), you can consider using ItemDataBound event. Here is a simple
sample which use repeater to display multiple html table. And I use
"ItemDatdaBound" event to set the background color of each table:


==========aspx page====================
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">

<ItemTemplate>
<table id="tb" runat="server">
<tr>
<td>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" />
</td>
</tr>
</table>
</ItemTemplate>

</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
SelectCommand="SELECT [id], [name] FROM
[numtable]"></asp:SqlDataSource>
</form>
==============code behind===============

Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
Repeater1.ItemDataBound
Dim tb As HtmlTable = e.Item.FindControl("tb")

If e.Item.ItemIndex Mod 2 = 0 Then
tb.Style(HtmlTextWriterStyle.BackgroundColor) = "yellow"
End If


End Sub
=================================

Here are some other web articles mentioned some samples of using
ItemDataBound event to customize repeater or datalist control:

#ASP.NET Tip: Use the ItemDataBound Event of a Repeater
http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/

#Formatting the DataList and Repeater Based Upon Data
http://www.asp.net/Learn/Data-Access/tutorial-30-vb.aspx


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
Reply-To: "Michael" <[email protected]>
From: "Michael" <[email protected]>
Subject: How to change the row color of the Repeater based on some condition?
Date: Thu, 19 Jun 2008 14:34:13 +0800
I have a repeater web control. Currently I want to change some row's color
based on defined condition. Is there any code sample demonstrating how to
accomplish it?

Thanks.
 
Back
Top