Alternating row colors on an "ordinary" table in ASP.NET 2

Discussion in 'ASP .Net' started by Guest, Jul 12, 2006.

  1. Guest

    Guest Guest

    I'm making admin forms. I'm wondering if there is a way to have the server
    programmatically assign alternating colors in a regular table (not a
    datalist control). I notice ASP.NET 2 offers a runat="server" attribute on
    tables, leading me to believe that this should be possible.

    Any ideas? I'm using C#.

    Thanks,
    -KF
    Guest, Jul 12, 2006
    #1
    1. Advertising

  2. Hello KF,

    You have surmised correctly. Using the runat="server" and giving the table
    an ID allows you to access it in server side code. HtmlTable's are composed
    of a HtmlTableRowCollection's. Iterating through the HtmlTableRowCollection
    items will allow you to set the background for the row through the BgColor
    property:

    // Iterate through the rows of the table.
    for (int i = 0; i <= Table1.Rows.Count - 1; i++)
    {

    // Update the properties of each row.
    Table1.Rows.BgColor = BgColorSelect.Value;
    Table1.Rows.BorderColor = BorderColorSelect.Value;
    Table1.Rows.Height = HeightSelect.Value;

    }

    Futhermore, you can iterate through the the table cells of the row as well
    and set properties for each cell (if you so wish).
    --
    enjoy - brians
    http://www.limbertech.com


    "" wrote:

    > I'm making admin forms. I'm wondering if there is a way to have the server
    > programmatically assign alternating colors in a regular table (not a
    > datalist control). I notice ASP.NET 2 offers a runat="server" attribute on
    > tables, leading me to believe that this should be possible.
    >
    > Any ideas? I'm using C#.
    >
    > Thanks,
    > -KF
    >
    >
    >
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=, Jul 12, 2006
    #2
    1. Advertising

  3. Sure. You just build an HtmlTable from scratch, and assign a different
    background color to each HtmlTableRow as you add it.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Professional Chicken Salad Alchemist

    What You Seek Is What You Get.


    <> wrote in message
    news:...
    > I'm making admin forms. I'm wondering if there is a way to have the server
    > programmatically assign alternating colors in a regular table (not a
    > datalist control). I notice ASP.NET 2 offers a runat="server" attribute on
    > tables, leading me to believe that this should be possible.
    >
    > Any ideas? I'm using C#.
    >
    > Thanks,
    > -KF
    >
    Kevin Spencer, Jul 12, 2006
    #3
  4. Create a CSS class to represent the odd or even records. Then during your
    building of the table assign the approprate class to the TR tag's class
    property. This will require you to keep track of if the current record is
    even or odd.

    --
    -Demetri


    "" wrote:

    > I'm making admin forms. I'm wondering if there is a way to have the server
    > programmatically assign alternating colors in a regular table (not a
    > datalist control). I notice ASP.NET 2 offers a runat="server" attribute on
    > tables, leading me to believe that this should be possible.
    >
    > Any ideas? I'm using C#.
    >
    > Thanks,
    > -KF
    >
    >
    >
    =?Utf-8?B?RGVtZXRyaQ==?=, Jul 12, 2006
    #4
  5. Guest

    Guest Guest

    Very complete answer, thank you very much, Brians.

    -KF


    "brians[MCSD]" <> wrote in message
    news:...
    > Hello KF,
    >
    > You have surmised correctly. Using the runat="server" and giving the table
    > an ID allows you to access it in server side code. HtmlTable's are
    > composed
    > of a HtmlTableRowCollection's. Iterating through the
    > HtmlTableRowCollection
    > items will allow you to set the background for the row through the BgColor
    > property:
    >
    > // Iterate through the rows of the table.
    > for (int i = 0; i <= Table1.Rows.Count - 1; i++)
    > {
    >
    > // Update the properties of each row.
    > Table1.Rows.BgColor = BgColorSelect.Value;
    > Table1.Rows.BorderColor = BorderColorSelect.Value;
    > Table1.Rows.Height = HeightSelect.Value;
    >
    > }
    >
    > Futhermore, you can iterate through the the table cells of the row as well
    > and set properties for each cell (if you so wish).
    > --
    > enjoy - brians
    > http://www.limbertech.com
    >
    >
    > "" wrote:
    >
    >> I'm making admin forms. I'm wondering if there is a way to have the
    >> server
    >> programmatically assign alternating colors in a regular table (not a
    >> datalist control). I notice ASP.NET 2 offers a runat="server" attribute
    >> on
    >> tables, leading me to believe that this should be possible.
    >>
    >> Any ideas? I'm using C#.
    >>
    >> Thanks,
    >> -KF
    >>
    >>
    >>
    Guest, Jul 12, 2006
    #5
  6. A good place for this sort of things is PreRender event.
    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]

    "brians[MCSD]" <> wrote in message
    news:...
    > Hello KF,
    >
    > You have surmised correctly. Using the runat="server" and giving the table
    > an ID allows you to access it in server side code. HtmlTable's are
    > composed
    > of a HtmlTableRowCollection's. Iterating through the
    > HtmlTableRowCollection
    > items will allow you to set the background for the row through the BgColor
    > property:
    >
    > // Iterate through the rows of the table.
    > for (int i = 0; i <= Table1.Rows.Count - 1; i++)
    > {
    >
    > // Update the properties of each row.
    > Table1.Rows.BgColor = BgColorSelect.Value;
    > Table1.Rows.BorderColor = BorderColorSelect.Value;
    > Table1.Rows.Height = HeightSelect.Value;
    >
    > }
    >
    > Futhermore, you can iterate through the the table cells of the row as well
    > and set properties for each cell (if you so wish).
    > --
    > enjoy - brians
    > http://www.limbertech.com
    >
    >
    > "" wrote:
    >
    >> I'm making admin forms. I'm wondering if there is a way to have the
    >> server
    >> programmatically assign alternating colors in a regular table (not a
    >> datalist control). I notice ASP.NET 2 offers a runat="server" attribute
    >> on
    >> tables, leading me to believe that this should be possible.
    >>
    >> Any ideas? I'm using C#.
    >>
    >> Thanks,
    >> -KF
    >>
    >>
    >>
    Eliyahu Goldin, Jul 13, 2006
    #6
    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. Ralph Snart
    Replies:
    2
    Views:
    758
    Ralph Snart
    Aug 3, 2003
  2. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    526
    darrel
    Aug 7, 2008
  3. ZelluX
    Replies:
    3
    Views:
    1,857
    Peter Otten
    Dec 1, 2008
  4. NancyASAP

    alternating item style in template row with table tags

    NancyASAP, Jul 12, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    169
    Fiaz Ali Saleemi
    Jul 14, 2005
  5. Newton

    Alternating table background colors

    Newton, Jun 12, 2004, in forum: ASP General
    Replies:
    1
    Views:
    126
    Roji. P. Thomas
    Jun 12, 2004
Loading...

Share This Page