之前有寫了一篇JavaScript的產生日曆
這篇是改成用ASP.NET寫的


private void CreateCalendar(int Year, int Month, DataTable DT)
{
DateTime whichDate = new DateTime(Year,Month,1);


string strCalendar = "";

// 每月日數陣列
int[] monthDays = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};

// 閏年判斷
int myYear = whichDate.Year;
int myMonth = whichDate.Month;
if ( ( (myYear % 4 == 0) && (myYear % 100 != 0) )
|| (myYear % 400 == 0) )
monthDays[1] = 29;


string [] myArray =
new string [7] {"Sunday","Monday","Tuesday","Wednesday"
,"Thursday","Friday","Saturday"};

string [] myArray2 = new string [7] {"0","1","2","3","4","5","6"};


// 該月第一天的星期
int day =
Convert.ToInt32(myArray2[Array.IndexOf(myArray, whichDate.DayOfWeek.ToString())]);


// 計算秀出時需要的格數
int total = monthDays[myMonth-1] + day;
int totalCells = total + ( (total%7 != 0) ? 7 - total%7 : 0  ); 


// 標題列
strCalendar ="<TABLE id=\"myCalendarTable\" class=\"myGrid\" style=\"border-collapse:
collapse\" cellSpacing=\"0\" cellPadding=\"4\" align=\"center\" width=\"100%\" border=\"0\">";
strCalendar += "<TR>";
strCalendar += "<TD class=\"GridHeading\" width=\"14%\">星期日</TD>";
strCalendar += "<TD class=\"GridHeading\" width=\"14%\">星期一</TD>";
strCalendar += "<TD class=\"GridHeading\" width=\"14%\">星期二</TD>";
strCalendar += "<TD class=\"GridHeading\" width=\"14%\">星期三</TD>";
strCalendar += "<TD class=\"GridHeading\" width=\"14%\">星期四</TD>";
strCalendar += "<TD class=\"GridHeading\" width=\"14%\">星期五</TD>";
strCalendar += "<TD class=\"GridHeading\" width=\"14%\">星期六</TD>";
strCalendar += "</TR>";


for (int i=0;i<totalCells;i++)
{
if ( i%7 == 0 )
{
strCalendar+="</TR><TR height=\"70px\">";
}


if ( i >= day && i < total )
{
int myDay = ((i-day)+1);
string myDateStr = whichDate.ToString("yyyy/MM/")  + myDay.ToString("D2");
if ( !DT.Rows.Contains(new DateTime(myYear,myMonth,myDay)) )
{
strCalendar += "<TD class=\"myItem\" onclick=\"addClass(this);\" >"+
myMonth.ToString("D2") + "/" + myDay.ToString("D2");
}
else
{
string myContentStr = "0";
DataRow rowFound;

// 設定DataTable的PrimaryKey,這樣之後才可以對這個DataTable作尋找內容的動作
DataColumn[] ColumnPrimaryKey = new DataColumn[1];
ColumnPrimaryKey[0] = DT.Columns["myDate"];
DT.PrimaryKey = ColumnPrimaryKey;

// 因為已經設定myDate為主鍵,他是DateTime型別的,故這邊用DateTime搜尋
rowFound = DT.Rows.Find(new DateTime(myYear,myMonth,myDay));
if( rowFound != null )
{
myContentStr = rowFound["whichNumber"].ToString();
}


strCalendar += "<TD class=\"myItem\" onclick=\"addClass(this);\">"+
myMonth.ToString("D2") + "/" + myDay.ToString("D2");
}
}
else
{
strCalendar += "<TD class=\"myItem\">&nbsp;";
}


strCalendar += "</TD>";
}

calendarDiv.InnerHtml = strCalendar;
}

welkingunther 發表在 痞客邦 PIXNET 留言(0) 人氣()