I read a blog about how to create a calendar, it's simple, but i still want to past it on my blog. OK, Let's go !
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write(DateTime.Now.Year+" . "+ DateTime.Now.Month);
CreateCalendar(DateTime.Now.Year, DateTime.Now.Month);
}
}
private void CreateCalendar(int year, int month)
{
int days = DateTime.DaysInMonth(year, month);//retrieve the days of month
DateTime dt = new DateTime(year,month,1);
int weeke = Convert.ToInt32(dt.DayOfWeek.ToString("d")); //get the week day of the first day in month
string[,] cal = new string[7,6];//store the date
for (int day = 1; day <= days; day++)
{
int x = (day + weeke - 1) % 7;
int y = (day + weeke - 1) / 7;
for (int j = 0; j < 6; j++)
{
for (int i = 0; i < 7; i++)
{
cal[x, y] = day.ToString();
}
}
}
System.Text.StringBuilder st = new System.Text.StringBuilder();
st.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">");
st.Append("<thead><th>星期天</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></thead>");
for (int i = 0; i < 6; i++)
{
st.Append("<tr>");
for (int j = 0; j < 7; j++)
{
st.Append("<td>" + cal[j, i] + "</td>");
}
st.Append("</tr>");
}
st.Append("</table>");
Response.Write(st);
Response.End();
}
}
Tuesday, November 11, 2008
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment