Pages

Free Hosting

Monday, October 17, 2011

Displaying Festival Names on Calendar control in ASP.Net

Tags : Festival Names,Calendar Control,Importance of the day,Asp.Net,C#.Net,SQL Server.

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 :
1.  using System; 
2.  using System.Configuration; 
3.  using System.Data; 
4.  using System.Linq; 
5.  using System.Web; 
6.  using System.Web.Security; 
7.  using System.Web.UI; 
8.  using System.Web.UI.HtmlControls; 
9.  using System.Web.UI.WebControls; 
10. using System.Web.UI.WebControls.WebParts; 
11. using System.Xml.Linq; 
12. using System.Data.SqlClient; 
13. public partial class _Default : System.Web.UI.Page 
14. { 
15. SqlConnection con; 
16. protected void Page_Load(object sender, EventArgs e) 
17. { 
18.  con = new SqlConnection(ConfigurationManager.ConnectionStrings["DotnetGuruConnectionString"].ConnectionString); 
19. } 
20. protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) 
21. { 
22.  CalendarDay day = (CalendarDay)e.Day; 
23.  SqlCommand cmd = new SqlCommand("select HolidayName,HolidayDate from Holidays", con); 
24.  con.Open(); 
25.  SqlDataReader dr = cmd.ExecuteReader(); 
26.   
27.  while (dr.Read()) 
28.  { 
29.      if (day.Date.ToString() == dr["HolidayDate"].ToString()) 
30.      { 
31.          TableCell cell = (TableCell)e.Cell; 
32.          string s1 = dr["HolidayName"].ToString(); 
33.          if (s1 != null) 
34.          { 
35.              cell.BackColor = System.Drawing.Color.LightGray; 
36.              cell.Controls.Add(new LiteralControl(" 
37. " + s1)); 
38.          } 
39.   
40.      } 
41.   
42.  } 
43.  con.Close(); 
44. } 
45. } 

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 :

 
1.  <FORM id=form1 runat="server"> 
2.  <DIV> 
3.    
4.      <ASP:CALENDAR id=Calendar1 size="8pt" width="300px" runat="server" showgridlines="True" height="200px" forecolor="#663399" names="Verdana" daynameformat="Shortest" borderwidth="1px" bordercolor="#FFCC66" backcolor="#FFFFCC" ondayrender="Calendar1_DayRender"> 
5.          <SELECTEDDAYSTYLE backcolor="#CCCCFF" bold="True"> 
6.          <SELECTORSTYLE backcolor="#FFCC66"> 
7.          <TODAYDAYSTYLE forecolor="White" backcolor="#FFCC66"> 
8.          <OTHERMONTHDAYSTYLE forecolor="#CC9966"> 
9.          <NEXTPREVSTYLE size="9pt" forecolor="#FFFFCC"> 
10.         <DAYHEADERSTYLE height="1px" backcolor="#FFCC66" bold="True"> 
11.         <TITLESTYLE size="9pt" forecolor="#FFFFCC" backcolor="#990000" bold="True"> 
12.     </TITLESTYLE> 
13.   
14. </DAYHEADERSTYLE></NEXTPREVSTYLE></OTHERMONTHDAYSTYLE></TODAYDAYSTYLE></SELECTORSTYLE></SELECTEDDAYSTYLE></ASP:CALENDAR></DIV> 
15. </FORM> 
<SPAN style="COLOR: rgb(51,51,255); FONT-SIZE: 130%; FONT-WEIGHT: bold"></SPAN>

No comments:

Post a Comment