How to change a table cell's background color?


H

HB

So I have a table in ASP.NET

<table runat="server" id="tblMyTable">
<tr>
<td id="cellMyCell">word</td>
</tr>
</table>

How do I reference the cell "cellMyCell" from the Vb.NET 1.1 code-behind to
change the background color of the cell?
 
Ad

Advertisements

H

HB

I forgot to add "without referencing row or cell numbers". I want to do it
by names, but the Rows(x).Cells(x) stuff just wants numbers. Yeah, I
could use an enum list but I'd like to know how to do this by "id" name.
 
E

Eliyahu Goldin

Write a little utility method that will take the cell id as a parameter and
loop through the Rows and Cells collections. Likely, you can use the
FindControl method to find a cell within a row.

Eliyahu
 
H

HB

It seems there must be normal way to just reference the cell by its ID name.
Anyone know?
 
Ad

Advertisements

M

Mark Rae

It seems there must be normal way to just reference the cell by its ID
name. Anyone know?

1) Make sure your <td /> includes the runat=server tag

2) Declare your tablecell object server-side - can't remember exactly how to
do it in VB.NET, but in C# it's

protected HtmlTableCell cellMyCell;

3) When you want to change the background colour, do something like:

cellMyCell.BackColor = Color.Red;
 

Top