Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Rows in ASP Tables 2

Status
Not open for further replies.

Terpsfan

Programmer
Dec 8, 2000
954
US
I would like to know how to format the rows in an asp table so that I get alternating rows of gray and white background. I see these on the web all the time, just don't know how to do it.
 
Hi there

the way i do it .. is in my stylesheet (.css file) i have some that looks like

.Alternate
{
BACKGROUND-COLOR: #cccccc
}

where #cccccc is an alternative color

then when i am printing a table i do this

while not rst.EOF
RecordCounter = RecordCounter + 1
If RecordCounter Mod 2 = 1 Then
Response.Write &quot;<TR class='Alternate'>&quot;
else
Response.Write &quot;<TR>&quot;
End If
'then print the current record

End

Transcend
 
Thanks for responding. So the style sheet is in a separate file that you call like a procedure? And does the style sheet look exactly like you just wrote? Sorry, I'm kinda new to web programming. Excuse my ignorance.
 
Hi there

a style sheet is a .css file that you should create for your site, you might want to look up style sheets and how to write them.
You reference it in the html header like so

<head>
<meta NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<link REL=&quot;stylesheet&quot; TYPE=&quot;text/css&quot; HREF=&quot;stylesheetname.css&quot;>

What I pasted earlier is only one part of my style sheet, it's too long to paste all of it :)

Transcend
 
And yes ... the .css file is basically a separate text file ...

transcend
 
You can also include these styles at the top of your html file in the head section like so:
Code:
<html>
<head>
<style>
   /* You can create your own classes */
   .row0{
      color: #eeeeee; /* Hex RBG Color for foreground*/
   }
   .row1{
      color: #aaaaee;
   }
   /* Or you can overide existing tag defaults */
   table{
      width:75%;
   }
</style>
</head>
<body>
<table>
   <tr class=&quot;row0&quot;>
      <td>
...

Or you can actually write style into a tag like so:
Code:
<table style=&quot;width:75%;border:1px solid #dddddd;&quot;>

Another way to do your alternating row colors is to use the counter inside your loop and assign one of two classes based no it, similar to above:
Code:
while not rst.EOF 
   RecordCounter = RecordCounter + 1
   Response.Write &quot;<tr class='row&quot; & RecordCounter mod 2 & &quot;'>&quot;
   
wend

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
This space has nothing in it, it's all ni your imagination
 
Thanks everyone for your reply, I'm still trying to understand this, which method to use and where.

HTML>
<HEAD>
<TITLE>Maryland Basketball Alumni <%=Now%></TITLE>
</HEAD>
<CENTER>
<H1>TERPS Players in the NBA!</H1>
<BODY BGCOLOR = &quot;BEIGE&quot; TEXT = &quot;RED&quot;>
<HR>
<IMG SRC=&quot;j0305651.wmf&quot; WIDTH=90 HEIGHT=90>
<IMG SRC=&quot;j0151209.wmf&quot; WIDTH=90 HEIGHT=90>

<%
dim objRec
dim SQL
dim objConn
SQL = &quot;SELECT * FROM qryNBA&quot;
Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
objConn.Open(&quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot; & Server.MapPath(&quot;\vbprogrammer38\db\MDSports.mdb&quot;))

Set objRec = objConn.Execute(SQL)

Response.Write &quot;<HR>&quot;
Response.Write &quot;<TABLE BORDER = 1 >&quot;
Response.Write &quot;<TR BGColor = Red Align = Center><TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;Last Name&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;First Name&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;Team&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;Games&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;Points&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;PPG&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;FG&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;FGA&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;FGP&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;RB&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;RPG&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;AST&quot; & &quot;</FONT></TD>&quot;
Response.Write &quot;<TD><B><FONT Size = 3 Color = Yellow>&quot; & &quot;APG&quot; & &quot;</FONT></TD></TR>&quot;
Do While Not objRec.EOF
Response.Write &quot;<TR Align = Center BGColor = White><TD><Font Color = Navy><B><I>&quot; & objRec(&quot;PlayerLname&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & objRec(&quot;PlayerFname&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & objRec(&quot;Team&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & objRec(&quot;Games&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & objRec(&quot;Points&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & FormatNumber(objRec(&quot;PPG&quot;),1) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & objRec(&quot;FG&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & objRec(&quot;FGA&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & FormatPercent(objRec(&quot;FGP&quot;),1) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & objRec(&quot;RB&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & FormatNumber(objRec(&quot;RPG&quot;),1) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B><I>&quot; & objRec(&quot;AST&quot;) & &quot;</TD>&quot;
Response.Write &quot;<TD><Font Color = Navy><B>&quot; & FormatNumber(objRec(&quot;APG&quot;),1) & &quot;</TD></TR>&quot;

objRec.MoveNext
Loop
Response.Write &quot;</TABLE>&quot;

objRec.Close
Set objRec = Nothing
objConn.Close
Set objConn = Nothing

%>
</BODY>
</CENTER>
</HTML>
 
Any of the methods would work. The easiest in this case would be to define some classes to replace all the font tags and so on you have, like so:
Code:
<HTML>
<HEAD>
<TITLE>Maryland Basketball Alumni <%=Now%></TITLE>
<STYLE>
   .colHead{
      font-size:3pt;
      color:yellow;
      font-weight:bold;
      background-color:red;
      text-align:center;
   }
   .colCell{
      font-weight:bold;
      font-style:italic;
      color:#222277; /* should be navyish */
      text-align:center;
   }
   .row_0{
      background-color:white;
   }
   .row_1{
      background-color:#dddddd; /* light gray */
   }
</STYLE>
</HEAD>
.
.
.
Response.Write &quot;<TABLE BORDER='1' cellspacing='0'>&quot;
Response.Write &quot;<TR>&quot;
Response.Write &quot;<TD class='colHead'>&quot; & &quot;Last Name&quot; & &quot;</TD>&quot;
Response.Write &quot;<TD class='colHead'>&quot; & &quot;First Name&quot; & &quot;</TD>&quot;
Response.Write &quot;<TD class='colHead'>&quot; & &quot;Team&quot; & &quot;TD>&quot;
.
.
.
Dim rowCtr
Do While Not objRec.EOF
   Response.Write &quot;<TR class='row_&quot; & rowCtr mod 2 & &quot;'>&quot;
   Response.Write &quot;<TD class='colCell'>&quot; & objRec(&quot;PlayerLname&quot;) & &quot;</TD>&quot;
        Response.Write &quot;<TD class='colCell'>&quot; & objRec(&quot;PlayerFname&quot;) & &quot;</TD>&quot;
        Response.Write &quot;<TD class='colCell'>&quot; & objRec(&quot;Team&quot;) & &quot;</TD>&quot;
.
.
.
   rowCtr = rowCtr + 1
   objRec.MoveNext
Loop
.
.
.

There you go. Now if you decide to change the color scheme or the font size, or even the font, you can do this in a single place. This also takes care of alternating row colors.

This also allows you to have finer control over the colors and look and feel. Take the border for instance, if you remove that border from your table tage you could add it to each cell or the whole table like so:
Code:
   .colHead{
      border:1px solid #666666;
      font-size:3pt;
      color:yellow;
      font-weight:bold;
      background-color:red;
      text-align:center;
   }

Border has three major attributes, color, width, and line type.
You can also split these up to define the left, right, top, or bottom border seperately.

Here is a good reference from one of the site partners:

Have fun :)

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
This space has nothing in it, it's all ni your imagination
 
Thanks Tarwan, that really did the trick. Now it makes sense to me. It's always a lot simpler than you first think. Happy Thanksgiving
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top