C# 利用repeater 的巢狀 實現一個自己想要的 日曆頁面

iDotNetSpace發表於2009-01-04

repeater 控制元件是一個很靈活的東東,本人在程式開發中就最愛用到它了!!!
很多朋友應該在使用 VS2005 自帶的 日曆控制元件的時候 都發現或多或少的 不能滿足自己的特殊需求

這個例項看似很簡單  但如果能夠家裡改善 將能很好的運用到自己的程式當中(至少比用JS寫個控制元件簡單方便,還能很好的很資料庫關聯開發),本程式碼只貼上出了日曆的實現,實際上我用到了四個repeater 的巢狀 實現的是一個日程管理的系統, 剩下的和資料庫有關聯,所以刪掉了!  希望對想使用日曆控制元件的朋友有一定的幫助,註釋寫的很清楚了,朋友們可以隨便改動 (程式有註釋的,希望大家貼上到VS 2005 中去看  很直觀 很清楚的)

 

aspx 頁面



    無標題頁
   
   
       
        function img_refresh(n){
            var date = document.getElementById("hdDate").value;
            document.location.href = "Default.aspx?&date=" + date + "&goto=" + n;
        }

   


   
       
           


               

                   

                   

                   

               

               

                   

                   

                   

                   

                   

                   

                   

               

               
                   
                   

                                                   
                       

                           
                               
                                   

                               
                           
                       

                   
                   
                   
                   

               
           
前一月後一月
星期日星期一星期二星期三星期四星期五星期六

                                       
                                           
                                               
                                               
                                           
                                           
                                               
                                           
                                       

                                                   
                                               
                                       
                                   

       
   

 

.CS檔案

 

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 bj = 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;");
                    }
                }
            }
        }

        ///


        /// 獲得本月 某一週的具體日期(不足七天的  比如第一週不是從週日開始的或者最後一週不是以週六結束的 )都以空時間 補充到 周詳細表中
        ///

        /// 本月的第幾周
        /// 返回本月某一週的 具體 日期 的一個集合
        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 bj = new DateTime();
            if (n == 1)//如果是第一週
            {
                bj = 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
            {//如果不是第一週  直接從第一天加七天
                bj = date.AddDays(1 - date.Day);
                int week = (int)obj.DayOfWeek;
                if (week > 0) bj = obj.AddDays((n - 1) * 7 - week);
                else bj = 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];
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-526509/,如需轉載,請註明出處,否則將追究法律責任。

相關文章