Sunday, March 6, 2011

Set table cell color from database

I'm overthinking this. I have colors stored in a database table, and I want to set the background of specific cells in a table to those colors. In other words:

<table>
    <tr>
        <td ???set color here???>
            ...content...
        </td>
        <td ???next color here???>
            ...next content...
        </td>
    </tr>
</table>

Originally I had Panels surrounding each piece of content and I set their background color in the code-behind, which worked fine until I had varying size panels, which threw off the layout. What's the easiest way to feed the color values from the database to the <td> element? Note that the colors are user-configurable, so I can't have them pre-defined in a CSS file.

From stackoverflow
  • Why not have the DB populate the CSS?

    .dark {
       background-color:[database field]
    }
    
    <td class='dark'></td>
    
  • You can make a custom CSS file with database data by creating a custom HttpHandler.
    But the simple way woud be:

    <td style="background-color:#000000">
    ...
    </td>
    

    with

    <td style='background-color:<%= GetCellColor() %>'>
    ...
    </td>
    
  • Is this a table with fixed number of rows/columns?

    You might use ASP style of code here.
    td backcolor="<%= MyColorProvider.FirstCellColor %>" .....

    Where MyColorProvider.FirstCellColor is the string representation of color (it could be a hex string as well).

    Eduardo Campañó : this is an old html sintax, it's better to use style="background-color:#000000", or a css stylesheet. And the correct old sintax is td bgcolor="#000000"
  • You could output a css file fromt the database when the application starts up, and then include the css file in the master page.

0 comments:

Post a Comment