没有使用JavaScript的日历控件
前台页面
aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NoJsCalendar.aspx.cs" Inherits="NoJsCalendar" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="NoJsCalendar" cellspacing="0" cellpadding="4" title="Calendar" border="0"
runat="server">
<tr>
<td align="center">
<asp:Button ID="ButtonPre" runat="server" Text="<" Width="24" />
</td>
<td align="center" colspan="5">
</td>
<td align="center">
<asp:Button ID="ButtonNxt" runat="server" Text=">" Width="24" />
</td>
</tr>
<tr>
<th align="center" abbr="Sunday" scope="col">Sun</th>
<th align="center" abbr="Monday" scope="col">Mon</th>
<th align="center" abbr="Tuesday" scope="col">Tue</th>
<th align="center" abbr="Wednesday" scope="col">Wed</th>
<th align="center" abbr="Thursday" scope="col">Thu</th>
<th align="center" abbr="Friday" scope="col">Fri</th>
<th align="center" abbr="Saturday" scope="col">Sat</th>
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
</table>
</div>
</form>
</body>
</html>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="NoJsCalendar" cellspacing="0" cellpadding="4" title="Calendar" border="0"
runat="server">
<tr>
<td align="center">
<asp:Button ID="ButtonPre" runat="server" Text="<" Width="24" />
</td>
<td align="center" colspan="5">
</td>
<td align="center">
<asp:Button ID="ButtonNxt" runat="server" Text=">" Width="24" />
</td>
</tr>
<tr>
<th align="center" abbr="Sunday" scope="col">Sun</th>
<th align="center" abbr="Monday" scope="col">Mon</th>
<th align="center" abbr="Tuesday" scope="col">Tue</th>
<th align="center" abbr="Wednesday" scope="col">Wed</th>
<th align="center" abbr="Thursday" scope="col">Thu</th>
<th align="center" abbr="Friday" scope="col">Fri</th>
<th align="center" abbr="Saturday" scope="col">Sat</th>
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
<tr>
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
<td align="center" />
</tr>
</table>
</div>
</form>
</body>
</html>
后台页面
cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class NoJsCalendar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["date"] = DateTime.Today;
Response.Write(ViewState["date"].ToString());
RefleshCalendar();
}
else
{
RefleshCalendar();
if (Request.Form["ButtonPre"] != null)
{
GotoPreMonth();
}
else if (Request.Form["ButtonNxt"] != null)
{
GotoNextMonth();
}
}
}
protected void GotoPreMonth()
{
ViewState["date"] = ((DateTime)ViewState["date"]).AddMonths(-1);
RefleshCalendar();
}
protected void GotoNextMonth()
{
ViewState["date"] = ((DateTime)ViewState["date"]).AddMonths(1);
RefleshCalendar();
}
protected void RefleshCalendar()
{
DateTime selectday = (DateTime)ViewState["date"];
int days = DateTime.DaysInMonth(selectday.Year, selectday.Month);
int startweekday = (int)selectday.AddDays(-selectday.Day + 1).DayOfWeek;
int dayinweek = startweekday;
int weekinmonth = 0;
for (int i = 2; i < NoJsCalendar.Rows.Count; i++)
{
for (int j = 0; j < 7; j++)
{
NoJsCalendar.Rows[i].Cells[j].Controls.Clear();
}
}
NoJsCalendar.Rows[0].Cells[1].InnerText = selectday.Month.ToString() + " / " + selectday.Year.ToString();
for (int i = 0; i < days; i++)
{
if (dayinweek > 6)
{
dayinweek = 0;
weekinmonth++;
}
Button b = new Button();
b.ID = "b" + (i + 1).ToString();
b.Text = (i + 1).ToString();
b.Width = 24;
b.Click += new EventHandler(SelectDate);
NoJsCalendar.Rows[weekinmonth + 2].Cells[dayinweek].Controls.Add(b);
dayinweek++;
}
}
protected void SelectDate(object sender, EventArgs e)
{
Button b = (Button)sender;
DateTime selectday = (DateTime)ViewState["date"];
int year = selectday.Year;
int month = selectday.Month;
int day = int.Parse(b.Text);
DateTime d = new DateTime(year, month, day);
Response.Write("You selected " + d.ToLongDateString());
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class NoJsCalendar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["date"] = DateTime.Today;
Response.Write(ViewState["date"].ToString());
RefleshCalendar();
}
else
{
RefleshCalendar();
if (Request.Form["ButtonPre"] != null)
{
GotoPreMonth();
}
else if (Request.Form["ButtonNxt"] != null)
{
GotoNextMonth();
}
}
}
protected void GotoPreMonth()
{
ViewState["date"] = ((DateTime)ViewState["date"]).AddMonths(-1);
RefleshCalendar();
}
protected void GotoNextMonth()
{
ViewState["date"] = ((DateTime)ViewState["date"]).AddMonths(1);
RefleshCalendar();
}
protected void RefleshCalendar()
{
DateTime selectday = (DateTime)ViewState["date"];
int days = DateTime.DaysInMonth(selectday.Year, selectday.Month);
int startweekday = (int)selectday.AddDays(-selectday.Day + 1).DayOfWeek;
int dayinweek = startweekday;
int weekinmonth = 0;
for (int i = 2; i < NoJsCalendar.Rows.Count; i++)
{
for (int j = 0; j < 7; j++)
{
NoJsCalendar.Rows[i].Cells[j].Controls.Clear();
}
}
NoJsCalendar.Rows[0].Cells[1].InnerText = selectday.Month.ToString() + " / " + selectday.Year.ToString();
for (int i = 0; i < days; i++)
{
if (dayinweek > 6)
{
dayinweek = 0;
weekinmonth++;
}
Button b = new Button();
b.ID = "b" + (i + 1).ToString();
b.Text = (i + 1).ToString();
b.Width = 24;
b.Click += new EventHandler(SelectDate);
NoJsCalendar.Rows[weekinmonth + 2].Cells[dayinweek].Controls.Add(b);
dayinweek++;
}
}
protected void SelectDate(object sender, EventArgs e)
{
Button b = (Button)sender;
DateTime selectday = (DateTime)ViewState["date"];
int year = selectday.Year;
int month = selectday.Month;
int day = int.Parse(b.Text);
DateTime d = new DateTime(year, month, day);
Response.Write("You selected " + d.ToLongDateString());
}
}