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

时间:2014-08-15 11:58:49

标签: c# asp.net calendar

我有代码可以在特定日历日更改字符串的颜色。

然而,它在我的后端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;
        }

是否有更合适的方法来设置文字颜色?

1 个答案:

答案 0 :(得分:0)

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

e.Cell.CssClass = color;

然后在你的css中:

.green{
   color: green;
}