以更好的方式更改日历内的文本颜色

本文关键字:文本 日历 颜色 更好 方式更 | 更新日期: 2023-09-27 18:18:12

我有代码来改变一个字符串的颜色在一个特定的日历天。

然而,在我的后端c#代码中有html代码似乎是一种"代码气味":

            if (CabinTable.Rows.Count > 0)
            {
                foreach (DataSet1.SummerCabinRow item in CabinTable.Rows)
                {
                    if (e.Day.Date >= item.StartDate && e.Day.Date <= item.EndDate)
                    {
                        e.Day.IsSelectable = false;
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(0xFF8F8F);
                        //Here I want to create a more professional code if its possible!!
                        string color = GetColor(item);
                        string bookerName = "<div style='"color:" + color + "'">" + item["Customer"] + "</div>";
                        e.Cell.Text = e.Day.Date.Day.ToString() + "<br/>" + bookerName;
                    }
                }
            }
        }
//Get the users unique color that comes from my database 
private string GetColor(DataSet1.SummerCabinRow item)
        {
            DataSet1.CustomerDataTable CustomerTable = new DataSet1TableAdapters.CustomerTableAdapter().GetData();
            DataSet1.CustomerRow currentUser = CustomerTable.Where(user => user.Name.Equals(item["Customer"])).FirstOrDefault();
            return currentUser.Color;
        }

有没有更合适的方法来设置文本颜色?

以更好的方式更改日历内的文本颜色

单元格应该是WebControl,这意味着它具有CssClass属性。您可以设置该属性,然后在css文件中添加适当的css规则:

e.Cell.CssClass = color;

然后在你的css中:

.green{
   color: green;
}