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

G

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
 
G

Guest

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).
 
K

Kevin Spencer

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.
 
G

Guest

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.
 
G

Guest

Very complete answer, thank you very much, Brians.

-KF


brians said:
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


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
 
E

Eliyahu Goldin

A good place for this sort of things is PreRender event.
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

brians said:
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


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
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top