System.Web.UI.HtmlControls Namespace HtmlTableCell Class
Sets or retrieves the background color of an HtmlTableCell control.
Inline |
<th | td bgcolor = strColor ... > |
Script |
HtmlTableCell.BgColor [ = strColor ] |
strColor |
A System.Drawing.Color that represents the background color of the control, using any of the standard .NET color identifiers. See Color Table. |
This property is read/write and returns an empty string if not set.
BgColor is used to specify the color under the content ( the background color ) of an HtmlTableCell control. This property is set using a System.Drawing.Color object, the possible range of values of which are specified in this workshop's Color Table.
The following example shows how to declaratively set the BgColor property of an HtmlTableCell control at design time.
<table cellspacing=1 width="92%" runat="server">
<tr>
<th>Table header</th></tr>
<tr>
<td bgcolor="ivory">Table data</td></tr>
</table>
The example below shows how to programmatically set the BgColor property of each HtmlTableCell at run time, depending on user input. The code also demonstrates how simple it is to retrieve the available colors in .NET ( via the KnownColor enum ), and dynamically add each to a selectable list.
void Page_Load ( object Source, EventArgs e ) {
if ( !IsPostBack ) {
Array colors = Enum.GetValues ( typeof ( KnownColor ) );
foreach ( KnownColor k in colors ) {
Color c = Color.FromKnownColor ( k );
if ( !c.IsSystemColor )
cellColorSelect.Items.Add ( c.Name );
}
}
// set the background color for each cell
for ( int r = 0; r <= myTable.Rows.Count - 1; r++ ) {
for ( int c = 0; c <= myTable.Rows [ r ].Cells.Count - 1; c++ ) {
myTable.Rows [ r ].Cells [ c ].BgColor = cellColorSelect.Value;
}
}
}
Show me
HtmlTableCell Members