前台:
<table cellpadding="0" cellspacing="0" class="AddTable">
<tr>
<td class="col1">今天日期</td>
<td class="col2"><%=DateTime.Now.ToShortDateString() %></td>
</tr>
<tr>
<td class="col1">月份选择</td>
<td class="col2">
<asp:Button ID="Button1" runat="server" Text="上月" OnCommand="GetData" CommandName="up" />
<asp:Button ID="Button2" runat="server" Text="下月" OnCommand="GetData" CommandName="down" />
</td>
</tr>
<tr>
<td class="col1">日历月份</td>
<td class="col2"><%=((DateTime)ViewState["Data"]).ToString("yyyy年MM月") %></td>
</tr>
<tr>
<td class="col1" colspan="2" style="padding:10px">
<table cellpadding="0" cellspacing="0" width="700px" id="Calendar">
<tr class="weektr">
<td>周日</td>
<td>周一</td>
<td>周二</td>
<td>周三</td>
<td>周四</td>
<td>周五</td>
<td>周六</td>
</tr>
</table>
<div style="width:703px">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="day" runat="server" style='<%# Eval("bgcolor","background:{0}") %>'>
<span title='<%# Eval("data") %>'><%# Eval("day") %></span>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</td>
</tr>
</table>
css:
.every{background:aliceblue; width:150px; border:solid 1px lightblue; text-align:center; margin:5px; float:left}
.AddTable{ border-collapse:collapse; width:720px; margin:0px auto; color:#3f6445; margin-top:20px; margin-bottom:20px}
.AddTable tr td{ border:solid 1px #80ab73; text-align:left; padding-left:5px}
.col1{ width:100px; text-align:right}
.col2{ width:600px; text-align:left}
.day{ width:94px; text-align:center; border:solid 1px #c0c0c0; height:50px; margin:2px; float:left}
.day span{ font-weight:bold; font-size:45px}
.weektr td{ height:30px; text-align:center; background:green; color:White;}
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Data"]=DateTime.Now;
Bind();
}
}
string[] week = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" };//日期
protected void Bind()
{
DateTime data = (DateTime)ViewState["Data"];
int year = data.Year;
int month = data.Month;
int days = DateTime.DaysInMonth(year, month);//获取总天数
DataTable daysdt = new DataTable();//实例一个表格
string[] col = { "day","Data","bgcolor"};
for (int i = 0; i < col.Length; i++)
{
DataColumn dc = new DataColumn();
dc.ColumnName = col[i];
daysdt.Columns.Add(dc);
}
DateTime start = Convert.ToDateTime(year.ToString() + "-" + month.ToString() + "-1");//月份第一天
for (int i = 0; i < week.Length; i++)//1号之前的置空
{
if (start.DayOfWeek.ToString().ToLower() == week[i] && i != 0)
{
for (int j = 0; j < i; j++)
{
DataRow dr = daysdt.NewRow();
dr["day"] = " ";
dr["data"] = " ";
dr["bgcolor"] = "White";
daysdt.Rows.Add(dr);
}
break;
}
}
for (int i = 0; i < days; i++)
{
DataRow dr = daysdt.NewRow();
dr["day"] = i + 1;
dr["Data"] = string.Format("{0}-{1}-{2}", year, month, i + 1);
dr["bgcolor"] = Convert.ToDateTime(dr["Data"].ToString()).ToShortDateString() == DateTime.Now.ToShortDateString() ? "tomato" : "White";
daysdt.Rows.Add(dr);
}
Repeater1.DataSource = daysdt;
Repeater1.DataBind();
}
protected void GetData(object sender, CommandEventArgs e)
{
DateTime data=(DateTime)ViewState["Data"];
if (e.CommandName == "up")
ViewState["Data"] = data.AddMonths(-1);
else
ViewState["Data"] = data.AddMonths(1);
Bind();
}
小胡原创
- 本文标题: ASP.NET C#日历的制作
- 文章分类:【.NET/Web】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
- 上一篇:word中页码不连续 的解决办法
- 下一篇: C#关键字区别之ref和out