Crista23
Crista23

Reputation: 3243

Add style programatically to table row

I am trying to add style to a table row in C# code. However, as simple as it may seem, I caanot find the proper way to do it. The code for my table is

<table width="100%" style="vertical-align:top; height:170px;" border="0" cellspacing="0" cellpadding="0"> 

    <tr id="trTasks" runat="server" onmouseover="this.bgColor='#eaeaea';" onmouseout="this.bgColor='#FFFFFF';" bgcolor="#FFFFFF"/>

</table>

I would like to set a different background color for trTasks when a condition is fullfiled. I've tried doing it like this:

trCompletedTasks.Attributes.Add("CssClass", "SelectedItem");

and

trCompletedTasks.Style.Add("CssClass", "SelectedItem");

but none of these seems to work.

Any suggestions would be much appreciated. Thank you very much.

Upvotes: 0

Views: 13151

Answers (2)

Brian Hamilton
Brian Hamilton

Reputation: 349

If you're just trying to get a row to change color when the mouse is over it, all modern browsers will let you do it just using CSS rather than on the server end. Just define a style with the desired background color for .yourtable tr:hover, and you'll be good to go.

See an example here: http://jsfiddle.net/Vdy6q/.

Upvotes: 1

Guffa
Guffa

Reputation: 700162

The HTML attribute is named class:

trCompletedTasks.Attributes.Add("class", "SelectedItem");

Upvotes: 3

Related Questions