Monday, July 27, 2009

Create Custom Calendar

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Request["date"] == null)
{
RenderCalendar(System.DateTime.Now);
}
else
{
if (Request["date"] != null)
{
RenderCalendar(Convert.ToDateTime(Request["date"]));
}
}
}

private void RenderCalendar(DateTime CurrentDate)
{
tblCalendar.Rows.Clear();
TableRow TitleRow = new TableRow();
TableCell PrevCell = new TableCell();
HyperLink hypPrev = new HyperLink();
hypPrev.Text = "<";
hypPrev.NavigateUrl = "Calendar.aspx?date=" + CurrentDate.AddMonths(-1).ToShortDateString();
TitleRow.Cells.Add(PrevCell);
PrevCell.Controls.Add(hypPrev);


TableCell DateHeaderCell = new TableCell();
DateHeaderCell.ColumnSpan = 5;
Label lblHeader = new Label();

lblHeader.Text = String.Format("{0:MMM}", CurrentDate) + " " + CurrentDate.Year.ToString();
DateHeaderCell.VerticalAlign = VerticalAlign.Middle;
DateHeaderCell.HorizontalAlign = HorizontalAlign.Center;
DateHeaderCell.Font.Bold = true;
TitleRow.Cells.Add(DateHeaderCell);
DateHeaderCell.Controls.Add(lblHeader);

TableCell NextCell = new TableCell();
HyperLink hypNext = new HyperLink();
hypNext.Text = ">";
hypNext.NavigateUrl = "Calendar.aspx?date=" + CurrentDate.AddMonths(1).ToShortDateString();

TitleRow.Cells.Add(NextCell);
NextCell.Controls.Add(hypNext);

tblCalendar.Rows.Add(TitleRow);

TableRow DayRow = new TableRow();
TableCell DayCellSun = new TableCell();
DayCellSun.Text = "Sun";
DayCellSun.Font.Bold = true;
DayRow.Cells.Add(DayCellSun);

TableCell DayCellMon = new TableCell();
DayCellMon.Text = "Mon";
DayCellMon.Font.Bold = true;
DayRow.Cells.Add(DayCellMon);

TableCell DayCellTue = new TableCell();
DayCellTue.Text = "Tue";
DayCellTue.Font.Bold = true;
DayRow.Cells.Add(DayCellTue);

TableCell DayCellWed = new TableCell();
DayCellWed.Text = "Wed";
DayCellWed.Font.Bold = true;
DayRow.Cells.Add(DayCellWed);

TableCell DayCellThu = new TableCell();
DayCellThu.Text = "Thu";
DayCellThu.Font.Bold = true;
DayRow.Cells.Add(DayCellThu);

TableCell DayCellFri = new TableCell();
DayCellFri.Text = "Fri";
DayCellFri.Font.Bold = true;
DayRow.Cells.Add(DayCellFri);

TableCell DayCellSat = new TableCell();
DayCellSat.Text = "Sat";
DayCellSat.Font.Bold = true;
DayRow.Cells.Add(DayCellSat);

tblCalendar.Rows.Add(DayRow);

String nDate = Convert.ToString(CurrentDate.ToShortDateString());

int year = CurrentDate.Year;
int month = CurrentDate.Month;
// int totalDays = DateTime.DaysInMonth(year,month);

String StartDate = month + "/1/" + year;
String FirstDay = Convert.ToDateTime(StartDate).DayOfWeek.ToString();

int currentDay = Convert.ToInt16(Convert.ToDateTime(nDate).Day);
// string CurrentWeekDay = Convert.ToDateTime(nDate).DayOfWeek.ToString();


int count = 0;
DateTime CountDate = Convert.ToDateTime(StartDate);
for (int i = 0; i <= 6; i++)
{
TableRow tr = new TableRow();
for (int j = 1; j <= 7; j++)
{
String CurrDay = GetDay(Convert.ToString(j));
TableCell td = new TableCell();
if (count >= 1 || FirstDay == CurrDay)
{
if (month == Convert.ToDateTime(CountDate).Month)
{
td.Text = Convert.ToDateTime(CountDate).Day.ToString();
count = count + 1;
DateTime dt = Convert.ToDateTime(CountDate).AddDays(1);
CountDate = Convert.ToDateTime(dt.ToShortDateString());

td.Attributes.Add("onClick", "Javascript:alert(" + count + ");");
}
}
else
{
td.Text = " ";
}
tr.Cells.Add(td);
}
tblCalendar.Rows.Add(tr);
}
}
private String GetDay(String Day)
{
switch (Day)
{
case "1":
return "Sunday";
case "2":
return "Monday";
case "3":
return "Tuesday";
case "4":
return "Wednesday";
case "5":
return "Thursday";
case "6":
return "Friday";
case "7":
return "Saturday";

}
return null;
}

No comments: