Wednesday, June 5, 2013

Displaying Festival Names on Calendar control in ASP.Net

Hi Friends,In this article i would like to explain how to display Festival Names on Calender control in ASP.Net.

* For this i took 1 Calendar control on to the page.

* I created a table with name(Holidays) in SQL Server 2005 & fields are HolidayName,HolidayDate.

* Please find the below table once :


* Then find the code for Calender_DayRender event : 
Source Code : 

Default.aspx.cs :

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    public partial class _Default : System.Web.UI.Page
    {
    SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
     con = new SqlConnection(ConfigurationManager.ConnectionStrings["DotnetGuruConnectionString"].ConnectionString);
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
     CalendarDay day = (CalendarDay)e.Day;
     SqlCommand cmd = new SqlCommand("select HolidayName,HolidayDate from Holidays", con);
     con.Open();
     SqlDataReader dr = cmd.ExecuteReader();

     while (dr.Read())
     {
         if (day.Date.ToString() == dr["HolidayDate"].ToString())
         {
             TableCell cell = (TableCell)e.Cell;
             string s1 = dr["HolidayName"].ToString();
             if (s1 != null)
             {
                 cell.BackColor = System.Drawing.Color.LightGray;
                 cell.Controls.Add(new LiteralControl("
    " + s1));
             }

         }

     }
     con.Close();
    }
    }

Design View : 

Default.aspx :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
 con = new SqlConnection(ConfigurationManager.ConnectionStrings["DotnetGuruConnectionString"].ConnectionString);
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
 CalendarDay day = (CalendarDay)e.Day;
 SqlCommand cmd = new SqlCommand("select HolidayName,HolidayDate from Holidays", con);
 con.Open();
 SqlDataReader dr = cmd.ExecuteReader();

 while (dr.Read())
 {
     if (day.Date.ToString() == dr["HolidayDate"].ToString())
     {
         TableCell cell = (TableCell)e.Cell;
         string s1 = dr["HolidayName"].ToString();
         if (s1 != null)
         {
             cell.BackColor = System.Drawing.Color.LightGray;
             cell.Controls.Add(new LiteralControl("
" + s1));
         }

     }

 }
 con.Close();
}
}


    * Then finally Build(F6) your application & run the application by pressing(F5) button.

    * Your out put will looks like as below : 
    Thank You...

    0 comments:

    Post a Comment