如何根据数据库中的信息更改表内容的背景颜色

时间:2022-02-18 08:23:46

I have a table that gets generated based on the information from the database. How do I change the background dynamically, lets say if repeat == 1 then then <td>repeat</td> should have a background of red.

我有一个基于数据库中的信息生成的表。如何动态更改背景,假设重复== 1然后重复 应该有红色背景。

while (reader.Read()) {
            int event_id = reader.GetInt32(0);
            string text = reader.GetString(1);
            DateTime eventStart = reader.GetDateTime(2);
            DateTime eventEnd = reader.GetDateTime(3);
            int repeat = reader.GetInt32(4);
            string Days = reader.GetString(5);
            //string Title = reader.GetString(2);
            htmlStr += "<tr><td BGCOLOR='#ffff00'>" + event_id + "</td><td>" + eventStart + 
                  "</td><td>" + eventEnd + "</td><td>" + repeat + "</td><td>" + Days + 
                  "</td></tr>";      
}

2 个解决方案

#1


0  

The best way would be to have a gridview and databind the reader, and then in databound event manage code to apply css to specific elements.

最好的方法是使用gridview并对读取器进行数据绑定,然后在数据绑定事件中管理代码以将css应用于特定元素。

Use this link for gridview databind - http://www.aspsnippets.com/Articles/How-to-bind-GridView-with-DataReader-in-ASPNet-using-C-and-VBNet.aspx

将此链接用于gridview databind - http://www.aspsnippets.com/Articles/How-to-bind-GridView-withANNReader-in-ASPNet-using-C-and-VBNet.aspx

Use this link for rowdatabound - http://www.dotnetgallery.com/kb/resource17-RowDatabound-event-tips-and-tricks-in-Gridview-control.aspx

使用此链接获取rowdatabound - http://www.dotnetgallery.com/kb/resource17-RowDatabound-event-tips-and-tricks-in-Gridview-control.aspx

#2


0  

If you wish to stick with creating the table using a string you can use some helper method like

如果你想坚持使用字符串创建表,你可以使用一些帮助方法

private string SwitchColor(string oldColor)
{
    string color = "#ffff00";
    if (oldColor == color)
        color = "#ff0000";
    return color;
}

and then switch between yellow and red for example, using a variable like this

然后在黄色和红色之间切换,例如,使用这样的变量

 string color = "#ffff00";
 while (reader.Read()) {
        color = SwitchColor(color);
        htmlStr += "<tr><td BGCOLOR='" + color + "'>".....
        }

You can also add class name on your column, instead of adding bgcolor attribute, and then handle it using css .

您还可以在列上添加类名,而不是添加bgcolor属性,然后使用css处理它。

#1


0  

The best way would be to have a gridview and databind the reader, and then in databound event manage code to apply css to specific elements.

最好的方法是使用gridview并对读取器进行数据绑定,然后在数据绑定事件中管理代码以将css应用于特定元素。

Use this link for gridview databind - http://www.aspsnippets.com/Articles/How-to-bind-GridView-with-DataReader-in-ASPNet-using-C-and-VBNet.aspx

将此链接用于gridview databind - http://www.aspsnippets.com/Articles/How-to-bind-GridView-withANNReader-in-ASPNet-using-C-and-VBNet.aspx

Use this link for rowdatabound - http://www.dotnetgallery.com/kb/resource17-RowDatabound-event-tips-and-tricks-in-Gridview-control.aspx

使用此链接获取rowdatabound - http://www.dotnetgallery.com/kb/resource17-RowDatabound-event-tips-and-tricks-in-Gridview-control.aspx

#2


0  

If you wish to stick with creating the table using a string you can use some helper method like

如果你想坚持使用字符串创建表,你可以使用一些帮助方法

private string SwitchColor(string oldColor)
{
    string color = "#ffff00";
    if (oldColor == color)
        color = "#ff0000";
    return color;
}

and then switch between yellow and red for example, using a variable like this

然后在黄色和红色之间切换,例如,使用这样的变量

 string color = "#ffff00";
 while (reader.Read()) {
        color = SwitchColor(color);
        htmlStr += "<tr><td BGCOLOR='" + color + "'>".....
        }

You can also add class name on your column, instead of adding bgcolor attribute, and then handle it using css .

您还可以在列上添加类名,而不是添加bgcolor属性,然后使用css处理它。