Tuesday, February 24, 2015

Displaying the speciality of the day in Calendar control in ASP.Net

Hi Friends,

In this article I would like to explain how to display specialties of day 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 & fields are HolidayName,HolidayDate.

You can change it accordingly. 

* 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["ConnectionString"].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 :


    <form id="form1" runat="server"> <div> <asp:calendar ondayrender="Calendar1_DayRender" id="Calendar1" runat="server" backcolor="#FFFFCC" bordercolor="#FFCC66" borderwidth="1px" daynameformat="Shortest" names="Verdana" size="8pt" forecolor="#663399" height="200px" showgridlines="True" width="300px"> <selecteddaystyle backcolor="#CCCCFF" bold="True"> <selectorstyle backcolor="#FFCC66"> <todaydaystyle backcolor="#FFCC66" forecolor="White"> <othermonthdaystyle forecolor="#CC9966"> <nextprevstyle size="9pt" forecolor="#FFFFCC"> <dayheaderstyle backcolor="#FFCC66" bold="True" height="1px"> <titlestyle backcolor="#990000" bold="True" size="9pt" forecolor="#FFFFCC"> </titlestyle> </dayheaderstyle></nextprevstyle></othermonthdaystyle></todaydaystyle></selectorstyle></selecteddaystyle></asp:calendar></div> </form> <span style="font-weight: bold; color: rgb(51, 51, 255);font-size:130%;"></span>
* Then finally Build(F6) your application & run the application by pressing(F5) button.

* Your out put will looks like as below :

 Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work, you can appreciate by leaving your comments. Stay tuned and stay connected for more technical updates."

No comments:

Post a Comment