Sample code for displaying festival names on calendar control in ASP.Net | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Sample code for displaying festival names on calendar control in ASP.Net

Friday

Displaying festival names on calendar control using c# in ASP.Net:

                                          This is an article for showing festival names on a specific date on calendar control. First create a SQL table with following fields HolidayName, HolidayDate. And fulfill the festival name in "HolidayName" field and festival date in "HolidayDate" field.

Then  took one Calendar control on to the page(Calendar.aspx)

Copy and  paste the following code into your Calendar.aspx file

 <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 Copy and  paste the following code into your Calendar.aspx.cs file

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["YourConnectionString"].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();
}
}
That's all finally build and run your page you will see a calender control with festival name.

0 comments:

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets