发个以前A的新浪轻博客的“我的365天”模块,新手可自由发挥哈,欢迎下载
我最近在A一个情侣网站,做到2个人(情侣)都在这一天登录这网站就在日历里面添加一对脚丫子(当然这个可以用css)这个模块的时候,想起了以前A的新浪轻博客,反正差不多,就改了下速度搞定了这个功能。后来一想其实也可以用来做类似的很多事,配合当下的弹窗插件(或者直接秃溜溜的链接跳转过去)可以做 比如预定/预约、记事本、提醒等一些功能。就这么个思路,不多说了,新手可以自由发挥的哈。部分截图如下:
所有源码在这里:http://download.csdn.net/detail/joyhen/5041462
主要代码:
using System;
using System.Data;
/// <summary>
/// 我的365天 by jon
/// <remarks>仿新浪的轻微博http://qing.weibo.com $$ http://qing.weibo.com/tag/365 365模块链接</remarks>
/// </summary>
public partial class Calender_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Data"] = DateTime.Now;
Bind();
}
}
//定义一个二维数组
string[,] week = { { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }, { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" } };
/// <summary>
/// 每个月的第一天前面的空格个数标记
/// </summary>
public int golb_tag = 0;
/// <summary>
/// 根据当前的ViewState时间得到日历
/// </summary>
protected void Bind()
{
DateTime data = (DateTime)ViewState["Data"];
int year = data.Year;
int month = data.Month;
int days = DateTime.DaysInMonth(year, month);//获取指定 year 中 month 的天数
DataTable daysdt = new DataTable();//实例一个表格等待填充
string[] col = { "day", "Data", "week", "class" };
for (int i = 0; i < col.Length; i++)
{
DataColumn dc = new DataColumn();
dc.ColumnName = col[i];
daysdt.Columns.Add(dc);
}
int tag = 0;//星期标记
DateTime start = Convert.ToDateTime(year.ToString() + "-" + month.ToString() + "-" + "1");//月份第一天
for (int i = 0; i < 7; i++)//得到在1号之前的置空个数
{
if (start.DayOfWeek.ToString().Substring(0, 3).ToLower() == week[0, i].ToLower() && i != 0)
{
golb_tag = i;
tag = i;
//for (int j = 0; j < i; j++)
// tag = tag + 1;
break;
}
}
string tempTime = string.Empty;
int tempTagInt;
tag = tag - 1;//数组长度为7,修改算法标记
for (int i = 0; i < days; i++)
{
DataRow dr = daysdt.NewRow();
dr["day"] = i + 1;
tempTime = string.Format("{0}-{1}-{2}", year, month, i + 1);
dr["Data"] = tempTime.Substring(0, tempTime.LastIndexOf('-'));
string dt1 = Convert.ToDateTime(tempTime).ToShortDateString();
string dt2 = DateTime.Now.ToShortDateString();
int tip = DateTime.Compare(Convert.ToDateTime(dt1), Convert.ToDateTime(dt2));
if (tip == 0)
dr["class"] = "today";//今天啦
else if (tip > 0)
dr["class"] = "coming";//提前记录
else if (tip < 0)
dr["class"] = "";
if (i == 19)
dr["class"] = "default";//测试,加入让每个月20号(i是从0开始的哦)这天预置
tag = tag + 1;//循环加一
tempTagInt = (tag < 7) ? tag : (tag % 7);//大于等于7求余
dr["week"] = week[1, tempTagInt] + "," + week[0, tempTagInt];
daysdt.Rows.Add(dr);
}
Repeater1.DataSource = daysdt;
Repeater1.DataBind();
}
/// <summary>
/// 上一个月的日历
/// </summary>
protected void d_prev_Click(object sender, EventArgs e)
{
DateTime data = (DateTime)ViewState["Data"];
ViewState["Data"] = data.AddMonths(-1);
Bind();
}
/// <summary>
/// 下一个月的日历
/// </summary>
protected void d_next_Click(object sender, EventArgs e)
{
DateTime data = (DateTime)ViewState["Data"];
ViewState["Data"] = data.AddMonths(1);
Bind();
}
}