using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Test.Web
{
public partial class _Default : System.Web.UI.Page
{
DateTime date = DateTime.Now;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["date"] != null)
{
date = Convert.ToDateTime(Request.QueryString["date"]).AddMonths(Convert.ToInt32(Request.QueryString["goto"]));
}
hdDate.Value = date.ToString();
if (!Page.IsPostBack)
{
lblDate.Text = date.ToString("yyyy年MM月");
BindWeek();
}
}
private void BindWeek()
{
DateTime obj = date.AddDays(1 - date.Day);//獲取本月第一天
int week = 1;
for (int i = 1; i < 32; i++)
{
DateTime newDay = obj.AddDays(i);
if (newDay.Month == obj.Month)//判斷天數加一後的時間是否還在本月內
{
if (newDay.DayOfWeek == 0) week++;//累加得到本月共有幾周
}
else break;
}
DataSet ds = new DataSet();//創建一個dtatset 用來作為repeater 的數據源
DataTable weekTable = new DataTable("weekTable");//創建一個dateTable 存放周表(即日歷的行數)
ds.Tables.Add(weekTable);
weekTable.Columns.Add("id");
weekTable.Columns.Add("Text");
for (int i = 1; i <= week; i++)
{
DataRow datarow = weekTable.NewRow();
datarow["id"] = i;
datarow["text"] = "第" + i.ToString() + "周";
ds.Tables["weekTable"].Rows.Add(datarow);
}
rptParent.DataSource = ds.Tables[0];//綁定第一個repeater 即日歷的行數
rptParent.DataBind();
}
protected void rptParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptChild = (Repeater)e.Item.FindControl("rptChild");
rptChild.DataSource = GetWeekTable(Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "id")));
rptChild.DataBind();
}
}
protected void rptChild_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//根據不同樣式 設置 添加按鈕 圖片路徑
HtmlImage img = (HtmlImage)e.Item.FindControl("imgAdd");
if (DataBinder.Eval(e.Item.DataItem, "week") != System.DBNull.Value) {
DateTime newTime = Convert.ToDateTime((DataBinder.Eval(e.Item.DataItem, "date")));
if (newTime.Date == DateTime.Now.Date)
{
HtmlTable td = (HtmlTable)e.Item.FindControl("colorCell");
td.Attributes.Add("style", "background-color:#F9E97D;");
}
}
}
}
/// <summary>
/// 獲得本月 某一周的具體日期(不足七天的 比如第一周不是從周日開始的或者最後一周不是以周六結束的 )都以空時間 補充到 周詳細表中
/// </summary>
/// <param name="n">本月的第幾周</param>
/// <returns>返回本月某一周的 具體 日期 的一個集合</returns>
private DataTable GetWeekTable(int n)
{
DataSet ds = new DataSet();
DataTable weekTable = new DataTable("weekTable");
ds.Tables.Add(weekTable);
weekTable.Columns.Add("week");
weekTable.Columns.Add("date");
weekTable.Columns[1].DataType = Type.GetType("System.DateTime");
DateTime obj = new DateTime();
if (n == 1)//如果是第一周
{
obj = date.AddDays(1 - date.Day);
int week = (int)obj.DayOfWeek;
if (week > 0)//如果不是周日 將補上空白
{
for (int i = week - 1; i >= 0; i--)
{
DataRow datarow = weekTable.NewRow();
ds.Tables["weekTable"].Rows.Add(datarow);
}
for (int i = week; i < 7; i++)
{
DataRow datarow = weekTable.NewRow();
datarow["week"] = i;
datarow["date"] = obj.AddDays(i - week);
ds.Tables["weekTable"].Rows.Add(datarow);
}
}
else
{//如果是周日 直接加上七天
for (int i = 0; i < 7; i++)
{
DataRow datarow = weekTable.NewRow();
datarow["week"] = i;
datarow["date"] = obj.AddDays(i);
ds.Tables["weekTable"].Rows.Add(datarow);
}
}
}
else
{//如果不是第一周 直接從第一天加七天
obj = date.AddDays(1 - date.Day);
int week = (int)obj.DayOfWeek;
if (week > 0) obj = obj.AddDays((n - 1) * 7 - week);
else obj = obj.AddDays((n - 1) * 7);
for (int i = 0; i < 7; i++)
{
DateTime newObj = obj.AddDays(i);
if (newObj.Month != obj.Month) break;//如果已經到下個月了 停止填充 進行(1)的判斷後 再填充空白所缺的天數
DataRow datarow = weekTable.NewRow();
datarow["week"] = i;
datarow["date"] = newObj;
ds.Tables["weekTable"].Rows.Add(datarow);
}
int count = ds.Tables["weekTable"].Rows.Count;//--------------(1)
if (ds.Tables["weekTable"].Rows.Count < 7)
{
for (int i = 0; i < 7 - count; i++)
{
DataRow datarow = weekTable.NewRow();
ds.Tables["weekTable"].Rows.Add(datarow);
}
}
}
return ds.Tables[0];
}
}
}