1.创建一个ASP.NET的Web窗体程序(C#)
2.在Web窗体中添加一个Label、TextBox和Button
<asp:Label ID="Label1" runat="server" Text="日期:"></asp:Label>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:Button ID="btnDate" runat="server" Text="..." />
3.添加一个Calendar控件
<asp:calendar id="cdrCalendar" runat="server"
backcolor="#ffffff" width="250px" height="200px"
font-size="12px" font-names="Arial" borderwidth="2px"
bordercolor="#000000" nextprevformat="shortmonth"
daynameformat="Shortest" Visible="False">
<TodayDayStyle ForeColor="White" BackColor="Black"></TodayDayStyle>
<NextPrevStyle Font-Size="12px" Font-Bold="True" ForeColor="#333333">
</NextPrevStyle>
<DayHeaderStyle Font-Size="12px" Font-Bold="True"></DayHeaderStyle>
<TitleStyle Font-Size="14px" Font-Bold="True" BorderWidth="2px"
ForeColor="#000055"></TitleStyle>
<OtherMonthDayStyle ForeColor="#CCCCCC"></OtherMonthDayStyle>
</asp:calendar>
4.在Button的Click事件中添加如下代码:
try
{
if (txtDate.Text.Trim() != "")
cdrCalendar.SelectedDate = Convert.ToDateTime(txtDate.Text);
}
catch
{
}
cdrCalendar.Visible = true; //显示 calendar.
5.在Calendar的SelectionChanged事件中添加如下代码:
//在TextBox中显示选择日期
txtDate.Text = string.Format("{0:yyyy-MM-dd}",cdrCalendar.SelectedDate);//string.Format格式化时间时,后面的内容必须为日期格式才可以
cdrCalendar.Visible = false; //隐藏Calendar控件
图 ASP.NET中Calendar控件效果图