ASP.Net Dynamic Calendar Week,Month,Year Wise

For Week Calendar Calendar
————————————————————————-
Decare For Color and MouseOver Event

string onmouseoverStyle = “this.style.backgroundColor=’#DDEDDF'”;
string onmouseoutStyle = “this.style.backgroundColor=’@BackColor'”;
string rowBackColor = String.Empty;
bool notTouched;

Declare and Fill Dataset From your Table and now you can Compare Date to Highlight Calendar cell and also highlight
particular Week From the Below Code
————————————————————————-
protected void calWeek_DayRender(object sender, DayRenderEventArgs e)
{

notTouched = true;
if (ds.Tables[0].Rows.Count > 0 && ds != null)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr[“Start_Date”].ToString() == e.Day.Date.ToString())
{
sb.Append(“<br>”);
sb.Append(“<br>”);
e.Cell.Controls.Add(new LiteralControl(sb.ToString()));
e.Cell.Controls.Add(new LiteralControl(“<a href=’frmEventMaster.aspx?eventid=” + dr[“EventID”].ToString() + “‘><font face=’Verdana’ size=’1′ color=’#715daa’>” + dr[“EventDesc”].ToString() + “</font></a>”));
e.Cell.BackColor = System.Drawing.Color.Cornsilk;
notTouched = false;
sb.Append(“<br>”);
sb.Append(“<br>”);
e.Cell.Controls.Add(new LiteralControl(sb.ToString()));
}
sb.Length = 0;
}
if (e.Day.IsOtherMonth)
{
notTouched = false;
e.Day.IsSelectable = false;
e.Cell.Text = “&nbsp;”;
e.Cell.BackColor = System.Drawing.Color.WhiteSmoke;
}
}

if (e.Day.IsOtherMonth)
{
notTouched = false;
e.Day.IsSelectable = false;
e.Cell.Text = “&nbsp;”;
e.Cell.BackColor = System.Drawing.Color.WhiteSmoke;
}

if (e.Day.Date == DateTime.Today)
{
notTouched = false;
e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml(“#F8E5EE”);
}

DateTime nextSunday = GetNextOccurenceOfDay(DateTime.Today, DayOfWeek.Sunday);
DateTime PrevSunday = GetPrevOccurenceOfDay(nextSunday, DayOfWeek.Sunday);

if (e.Day.Date >= PrevSunday && e.Day.Date < nextSunday)
{
e.Cell.ForeColor = System.Drawing.Color.BlueViolet;
}
else
{
e.Cell.Visible = false;
}

#region “Calendar Mouse Over Effect”

if (notTouched && !e.Day.IsWeekend)
{
e.Cell.Attributes.Add(“onmouseover”, onmouseoverStyle);
e.Cell.Attributes.Add(“onmouseout”, onmouseoutStyle.Replace(“@BackColor”, rowBackColor));
}
#endregion

}
—————————————————————————-
Here is the code For Current Week Dispaly

#region “Get Next and Previous Sunday”
public DateTime GetNextOccurenceOfDay(DateTime value, DayOfWeek dayOfWeek)
{
int daysToAdd = dayOfWeek – value.DayOfWeek;
if (daysToAdd < 1)
{
daysToAdd += 7;
}
return value.AddDays(daysToAdd);
}

public DateTime GetPrevOccurenceOfDay(DateTime value, DayOfWeek dayOfWeek)
{
int daysToAdd = dayOfWeek – value.DayOfWeek;
if (daysToAdd < 1)
{
daysToAdd -= 7;
}
return value.AddDays(daysToAdd);
}
#endregion
————–Month  Calendar————————————
Decare For Color and MouseOver Event

string onmouseoverStyle = “this.style.backgroundColor=’#DDEDDF'”;
string onmouseoutStyle = “this.style.backgroundColor=’@BackColor'”;
string rowBackColor = String.Empty;
bool notTouched;

Declare and Fill Dataset From your Table and now you can Compare Date to Highlight Calendar cell
————————————————————————-
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
notTouched = true;
if (ds.Tables[0].Rows.Count > 0 && ds != null)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr[“Start_Date”].ToString() == e.Day.Date.ToString())
{
sb.Append(“<br>”);
sb.Append(“<br>”);
e.Cell.Controls.Add(new LiteralControl(sb.ToString()));
e.Cell.Controls.Add(new LiteralControl(“<a href=’frmEventMaster.aspx?eventid=” + dr[“EventID”].ToString() + “‘><font face=’Verdana’ size=’1′ color=’#715daa’>” + dr[“EventDesc”].ToString() + “</font></a>”));
e.Cell.BackColor = System.Drawing.Color.Cornsilk;
notTouched = false;
sb.Append(“<br>”);
sb.Append(“<br>”);
e.Cell.Controls.Add(new LiteralControl(sb.ToString()));
}
sb.Length = 0;
}
if (e.Day.Date == DateTime.Today)
{
notTouched = false;
e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml(“#F8E5EE”);
}

if (e.Day.IsOtherMonth)
{
notTouched = false;
e.Day.IsSelectable = false;
e.Cell.Text = “&nbsp;”;
e.Cell.BackColor = System.Drawing.Color.WhiteSmoke;
}
}

#region “Calendar Mouse Over Effect”

if (notTouched && !e.Day.IsWeekend)
{
e.Cell.Attributes.Add(“onmouseover”, onmouseoverStyle);
e.Cell.Attributes.Add(“onmouseout”, onmouseoutStyle.Replace(“@BackColor”, rowBackColor));
}
#endregion

}
#endregion
————————————————————————-Yeat Calendar Dynamic Calendar For Given
Dynamic Generate Calendar on Given Month

//Here TextBox1.Text is Months Behind and  TextBox2.Text is Months Ahead
This is Dynamically Generated Calendar So Put followinf Code in Page_Load or Page_Init
————————————————————
#region “Year Calendar”
if (TextBox1.Text == “” && TextBox2.Text == “”)
{
TextBox1.Text = “1”;
TextBox2.Text = “1”;
}

if (TextBox1.Text != “” && TextBox2.Text != “”)
{
lblCal.Text = “Calendar Settings: You are viewing ” + TextBox1.Text + ” month behind ” + DateTime.Now.ToString(“MMMM-yyyy”) + ”  and  ” + TextBox2.Text + ” month ahead.”;

ArrayList a = new ArrayList();
DateTime CurrentDate = DateTime.Now;
DateTime startdate = CurrentDate.AddMonths(-int.Parse(TextBox1.Text.ToString()));
DateTime EndDate = CurrentDate.AddMonths(int.Parse(TextBox2.Text.ToString()));

ViewState[“Month”] = startdate.Year;
for (DateTime WorkDate = startdate; WorkDate <= EndDate; WorkDate = WorkDate.AddMonths(1))
{
Calendar c = new Calendar();
c = new Calendar();
a.Add(WorkDate.Month);
DateTime dt = WorkDate;
c.VisibleDate = dt;
c.SelectedDate = dt;
c.ShowNextPrevMonth = false;
c.Font.Name = “Verdana”;
c.Width = 764;
c.Font.Size = 9;
c.NextMonthText = “”;
c.PrevMonthText = “”;
//c.SelectedDayStyle.BackColor = System.Drawing.Color.Navy;
c.SelectedDayStyle.ForeColor = System.Drawing.Color.Green;
c.TitleStyle.BackColor = System.Drawing.Color.Gainsboro;
c.TitleStyle.Font.Bold = true;
c.TitleStyle.Font.Size = 10;
c.DayStyle.Height = 40;
c.DayStyle.Width = 14;
c.ShowGridLines = true;
c.BorderColor = System.Drawing.Color.MidnightBlue;
c.DayRender += new DayRenderEventHandler(this.CalendarYear_DayRender);
c.SelectionChanged += new EventHandler(this.Selection_Change);
c.DayStyle.Width = 125;
pnlYearCal.Controls.Add(c);

}
}
#endregion

One thought on “ASP.Net Dynamic Calendar Week,Month,Year Wise

  1. Wow, i have been searching high and low for this command:

    c.DayRender += new DayRenderEventHandler(this.CalendarYear_DayRender);

    I like to create everything in code, dynamically, and not use the asp crap in the .aspx file….that way the same page can completely and dynamically change.

Leave a comment