Table cell control

modularbeing

Regular
Joined
Jan 8, 2004
Messages
63
Hi, I have an asp table that i have hard coded all the rows and cells in the aspx file and I need to change the background color of one of the cells programmatically and I cant seem to find a way to reference the table cell control by its ID. Anyone know of a way I can do this?

thanks!
 
When you say you have an "asp table" I'm assuming you've setup something like this in your aspx page:

Code:
<asp:table id="tblTest" runat="server">
<asp:tablerow id="trTest" runat="server">
<asp:tablecell id="tdTest" runat="server">Pookie!</asp:tablecell>
</asp:tablerow>
</asp:table>

I guess it might depend on whether you use the web form designer or do your aspx/html raw (like I do)....but in your code behind there'd need to be a declaration for the asp:tablecell. For example (C#):

Code:
protected System.Web.UI.WebControls.TableCell tdTest;

This declaration may or may not exist for your asp:table depending on how you've done things. Some asp.net controls don't actually have to be declared in the code-behind to render properly. I don't use the designer and I never use asp:tables so I'm not sure exactly what would be declared via the designer.

As long as the tablecell control is declared in your code behind you can manipulate it in code behind:

Code:
tdTest.BackColor = Color.FromArgb(128, 128, 128);

As a side note, I personally just use regular html tables in my aspx pages. I tend to lean towards repeaters for building up tables via codebehind. (I'm a repeater junkie.)

I would do something more like this:

Code:
<table>
<tr>
    <td id="tdTest" runat="server">
</tr>
</table>

...kind of straight html with a slight twist - the table cell (td element) is declared with an id and a runat=server...

...because you can manipulate just about anything from codebehind and the only thing I want to manipulate in that example is that one table cell. In the code behind that html element gets declared as an 'htmlcontrols' instead of a 'webcontrols':

Code:
protected System.Web.UI.HtmlControls.HtmlTableCell tdTest;

And the color manipulation is a little bit different.

Code:
tdTest.BgColor = "#EFEFEF";

*shrug* Basically two different ways to do the same thing. You lose out on a little of the OOP support with my usual way (color set as a string instead of using a color object) but for the most part the WebControls.TableCell and HtmlControls.HtmlTableCell expose the exact same properties.

Paul
 
The htmltablecell control does not work because the actual control on the page is a tablecell control so I get a parser error. The other way of declaring it as a TableCell does not produce an error but does not produce a background color either :o , Im not sure why this will not work because I have done this before with other controls....I dont know if its just because this is a asp table and .net processes those differently or what.

thanks
 
Well oddly, the backcolor property doesnt do a thing, but if I add an attribute to the control for the background color then it works fine. strange but it works.

thanks for your help.
 
Back
Top