ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C# 利用repeater 的嵌套 实现一个自己想要的 日历页面

C# 利用repeater 的嵌套 实现一个自己想要的 日历页面

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-01-04 15:50:38 0 删除 编辑

repeater 控件是一个很灵活的东东,本人在程序开发中就最爱用到它了!!!
很多朋友应该在使用 VS2005 自带的 日历控件的时候 都发现或多或少的 不能满足自己的特殊需求

这个实例看似很简单  但如果能够家里改善 将能很好的运用到自己的程序当中(至少比用JS写个控件简单方便,还能很好的很数据库关联开发),本代码只粘贴出了日历的实现,实际上我用到了四个repeater 的嵌套 实现的是一个日程管理的系统, 剩下的和数据库有关联,所以删掉了!  希望对想使用日历控件的朋友有一定的帮助,注释写的很清楚了,朋友们可以随便改动 (程序有注释的,希望大家粘贴到VS 2005 中去看  很直观 很清楚的)

 

aspx 页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test.Web._Default" %>



    无标题页
   
   
       
        function img_refresh(n){
            var date = document.getElementById("hdDate").value;
            document.location.href = "Default.aspx?&date=" + date + "&goto=" + n;
        }

   


   
       
           


               
                   

                   

                   

               

               

                   

                   

                   

                   

                   

                   

                   

               

               
                   
                   

                                                   
                       

                           
                               
                                   

                               
                           
                       

                   
                   
                   
                   

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

                                       
                                           
                                               
                                               
                                           
                                           
                                               
                                           
                                       
<%#DataBinder.Eval(Container.DataItem, "date", "{0:dd}")%>

                                                   
                                               
                                       
                                   

       
   

 

.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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5321210