如何在asp.net中的日历控件上以红色显示公共假日?

时间:2014-10-21 09:47:54

标签: c# asp.net calendar

我在数据库中填写了10个公共假期表,我想在asp.net中的日历控制中显示红色的日期,我在星期天,星期六实现它,但无法弄清楚如何解决10公众假期

表插入脚本:

Declare @Year int
Set @Year = (Select Year(Getdate()))

INSERT INTO [dbo].[GazettedHolidays]
Select 1, GETDATE(), 'Kashmir Day', '05 Feb' +CAST(@Year as VARCHAR(4)) ,' Kashmir Day',1
Union All
Select 1, GETDATE(), 'Pakistan Day', '23 Mar'+CAST(@Year as VARCHAR(4)), ' Pakistan Day',1
Union All
Select 1, GETDATE(), 'Labour Day', '01 May' +CAST(@Year as VARCHAR(4)), ' Labour Day',1
Union All
Select 1, GETDATE(), 'Independence Day', '14 Aug' +CAST(@Year as VARCHAR(4)), 'Independence Day',1
Union All
Select 1, GETDATE(), 'Iqbal day', '09 Nov' +CAST(@Year as VARCHAR(4)),' Iqbal Day',1
Union All
Select 1, GETDATE(), 'Quaid-e-Azam Day', '25 Dec' +CAST(@Year as VARCHAR(4)),'Quaid-e-Azam Day',1       
GO

asp.net代码:

public partial class _Default : System.Web.UI.Page

{

    SqlConnection con = new SqlConnection("server=.;database=mcnsolutions;uid=sa;pwd=wintellect");//for connection

    SqlDataAdapter da;

    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)

    {

        da = new SqlDataAdapter("select * from holidays", con);

        da.Fill(ds);//we fill dataset ds from all the data of holidays table

    } 


    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)

    {

        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)

        {

            if (ds.Tables[0].Rows[i].ItemArray.Contains(e.Day.Date))

            {

                if (e.Day.Date.DayOfWeek == DayOfWeek.Saturday || e.Day.Date.DayOfWeek == DayOfWeek.Sunday)

                {

                    e.Cell.ToolTip = ds.Tables[0].Rows[i][1].ToString();

                }

                else

                {

                    e.Cell.ForeColor = Color.White;

                    e.Cell.BackColor = Color.Red;

                    e.Cell.ToolTip = ds.Tables[0].Rows[i][1].ToString();//for tooltip

                }

            }

1 个答案:

答案 0 :(得分:0)

定义公开列表

List<DateTime> holidayList = null;

获取holidayList的方法

 private List<DateTime> GetPublicHolidays()
{
    List<DateTime> list = new List<DateTime>();
    //populate from database or other sources
    list.Add(new DateTime(2014, 01, 01));
    list.Add(new DateTime(2014, 02, 02));
    list.Add(new DateTime(2014, 02, 14));
    list.Add(new DateTime(2014, 03, 02));
    list.Add(new DateTime(2014, 03, 14));
    list.Add(new DateTime(2014, 04, 02));
    list.Add(new DateTime(2014, 04, 14));
    list.Add(new DateTime(2014, 05, 02));
    list.Add(new DateTime(2014, 05, 14));
    list.Add(new DateTime(2014, 06, 02));
    list.Add(new DateTime(2014, 06, 14));
    list.Add(new DateTime(2014, 07, 02));
    list.Add(new DateTime(2014, 07, 14));
    list.Add(new DateTime(2014, 08, 02));
    list.Add(new DateTime(2014, 08, 14));
    list.Add(new DateTime(2014, 09, 02));
    list.Add(new DateTime(2014, 09, 14));
    list.Add(new DateTime(2014, 10, 02));
    list.Add(new DateTime(2014, 10, 14));
    list.Add(new DateTime(2014, 11, 02));
    list.Add(new DateTime(2014, 11, 14));
    list.Add(new DateTime(2014, 12, 02));
    list.Add(new DateTime(2014, 12, 14));
    return list;
}

在pageload事件期间使用值填充列表

protected void Page_Load(object sender, EventArgs e)
{

        holidayList = GetPublicHolidays();

}

将此代码放在您的Calander控件的DayRender事件中

if (holidayList.Contains(e.Day.Date) || e.Day.IsWeekend)
    {
        e.Cell.BackColor = System.Drawing.Color.Red;
        e.Cell.ForeColor = System.Drawing.Color.White;
        e.Day.IsSelectable = false;
    }