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

Discussion in 'ASP .Net' started by Michael, Jun 19, 2008.

  1. Michael

    Michael Guest

    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.
    Michael, Jun 19, 2008
    #1
    1. Advertising

  2. Michael

    Munna Guest

    Munna, Jun 19, 2008
    #2
    1. Advertising

  3. 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:
    .

    ==================================================
    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" <>
    >From: "Michael" <>
    >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.
    >
    >
    Steven Cheng [MSFT], Jun 19, 2008
    #3
  4. Michael

    Michael Guest

    Thanks very much for both of you. It is clear now.

    "Steven Cheng [MSFT]" <> wrote in message
    news:...
    > 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:
    > .
    >
    > ==================================================
    > 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" <>
    >>From: "Michael" <>
    >>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.
    >>
    >>

    >
    Michael, Jun 20, 2008
    #4
  5. 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:
    .

    ==================================================
    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" <>
    >From: "Michael" <>
    >References: <O1oN$>

    <>
    >In-Reply-To: <>
    >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 [MSFT]" <> wrote in message
    >news:...
    >> 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:
    >> .
    >>
    >> ==================================================
    >> 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" <>
    >>>From: "Michael" <>
    >>>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.
    >>>
    >>>

    >>

    >
    >
    Steven Cheng [MSFT], Jun 20, 2008
    #5
    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. george d lake

    How to change the color of a row (Repeater)?

    george d lake, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    609
    george d lake
    Jul 8, 2003
  2. =?Utf-8?B?aWdvdHlvdXJkb3RuZXQ=?=

    change row color of empty row

    =?Utf-8?B?aWdvdHlvdXJkb3RuZXQ=?=, Oct 19, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    448
    Walter Wang [MSFT]
    Oct 24, 2006
  3. Alan Z. Scharf

    Change color of Item Template TextBox on Condition

    Alan Z. Scharf, Aug 26, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    262
    Alan Z. Scharf
    Aug 29, 2003
  4. 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,591
    Jeffrey Tan[MSFT]
    Jan 21, 2004
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    405
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page