.aspx界面
<script language="javascript" type="text/javascript">
function getAbsolutePosition(element)
{
var point = { x: element.offsetLeft, y: element.offsetTop };
if (element.offsetParent)
{
var parentPoint = this.getAbsolutePosition(element.offsetParent);
point.x += parentPoint.x;
point.y += parentPoint.y;
}
return point;
};
function myclick()
{
var myTextbox=document.getElementById("myRegisterTime");
var point1 = getAbsolutePosition(myTextbox);
document.getElementById("caDiv").style.display="block";
document.getElementById("caDiv").style.left=point1.x;
document.getElementById("caDiv").style.top=point1.y;
}
function divonclick()
{
document.getElementById("caDiv").style.display="none";
}
</script>
添加TextBox与Calendar控件
<asp:TextBox ID="myRegisterTime" runat="server" OnDataBinding="BindDate"></asp:TextBox>
<div id="caDiv" style="display:none;position: absolute;" οnclick="divonclick()">
<asp:Calendar ID="Calendar1" runat="server" BackColor="White"
BorderColor="#3366CC" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#003399" Height="200px" Width="220px"
onselectionchanged="Calendar1_SelectionChanged" BorderWidth="1px"
CellPadding="1" DayNameFormat="Shortest">
<SelectedDayStyle BackColor="#009999" ForeColor="#CCFF99" Font-Bold="True" />
<SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
<WeekendDayStyle BackColor="#CCCCFF" />
<TodayDayStyle BackColor="#CCCCCC" />
<OtherMonthDayStyle ForeColor="#999999" />
<NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
<DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
<TitleStyle BackColor="#003399" Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF"
BorderColor="#3366CC" BorderWidth="1px" Height="25px" />
</asp:Calendar>
</div>
.cs界面
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myRegisterTime.Attributes.Add("onfocus", "javascript:myclick()");
}
}
//日历事件
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
if (Calendar1.SelectedDate > DateTime.Now.AddDays(-1.0))
myRegisterTime.Text = Calendar1.SelectedDate.ToLongDateString();
else
{
//WebHelper.Alert("日期无效,请重选!");
Calendar1.SelectedDate = DateTime.Now;
}
}
效果图展示:
TextBox控件中只能显示当前及以后的日期,对于已经过去的日期则不能选择