VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Objective-C编程 >
  • c#利用repeater的嵌套实一个自己想要的日历页面

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test.Web._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>无标题页</title>
  <script type="text/javascript" src="../javascript/PopupWindow.js"></script>
  <script type="text/javascript">
    function img_refresh(n){
      var date = document.getElementById("hdDate").value;
      document.location.href = "Default.aspx?&date=" + date + "&goto=" + n;
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
    <input type="hidden" runat="server" id="hdDay" />
      <table width="100%" height="100%" border="1" style="border:solid 1px #cccccc; border-collapse:collapse; table-layout:fixed;" rules="all" cellspacing="0" cellpadding="2">
        <tr class="tdTitle0" height="35px">
          <td align="center"><a href="javascript:img_refresh(-1);" >前一月</a></td>
          <td align="center" colspan="5" style="font-weight:bold;"><asp:Label ID="lblDate" runat="server"></asp:Label></td>
          <td align="center"><a href="javascript:img_refresh(1);" >后一月</a></td>
        </tr>
        <tr style="background-color:#FFFFCC" height="25px">
          <td align="center">星期日</td>
          <td align="center">星期一</td>
          <td align="center">星期二</td>
          <td align="center">星期三</td>
          <td align="center">星期四</td>
          <td align="center">星期五</td>
          <td align="center">星期六</td>
        </tr>
        <asp:Repeater ID="rptParent" runat="server" OnItemDataBound="rptParent_ItemDataBound">
          <HeaderTemplate>
          </HeaderTemplate>
          <ItemTemplate>                
            <tr>
              <asp:Repeater ID="rptChild" runat="server" OnItemDataBound="rptChild_ItemDataBound">
                <ItemTemplate>
                  <td>
                    <table id="colorCell" runat="server" width="100%" height="100%">
                      <tr>
                        <td align="left" height="25px" valign="middle"><%#DataBinder.Eval(Container.DataItem, "date", "{0:dd}")%></td>
                        <td align="right"></td>
                      </tr>
                      <tr>
                        <td colspan="2" valign="top">
                        </td>
                      </tr>
                    </table>                    
                  </td>
                </ItemTemplate>
              </asp:Repeater>
            </tr>
          </ItemTemplate>
          <FooterTemplate>
          </FooterTemplate>
        </asp:Repeater>
      </table>
    <input type="hidden" id="hdDate" runat="server" />
  </form>
</body>
</html>
  
.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 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];
    }
  }
}



相关教程