repeater 控件是一個很靈活的東東,本人在程序開發中就最愛用到它了!!!
很多朋友應該在使用 VS2005 自帶的 日歷控件的時候 都發現或多或少的 不能滿足自己的特殊需求
這個實例看似很簡單 但如果能夠家裡改善 將能很好的運用到自己的程序當中(至少比用JS寫個控件簡單方便,還能很好的很數據庫關聯開發),本代碼只粘貼出了日歷的實現,實際上我用到了四個repeater 的嵌套 實現的是一個日程管理的系統, 剩下的和數據庫有關聯,所以刪掉了! 希望對想使用日歷控件的朋友有一定的幫助,注釋寫的很清楚了,朋友們可以隨便改動 (程序有注釋的,希望大家粘貼到VS 2005 中去看 很直觀 很清楚的)
<%@ 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];
}
}
}