最简单的做是可以如下
1、新建一个aspx页面,把calender控件拉进来。
2、第一步是外观设置,这个根据你的需要,只需对它的相关属性做一些调整即可。可以调整如下图:
属性设置如下:
代码如下 | 复制代码 |
<asp:calendar id="Calendar1" CellPadding="2" Width="160px" TitleStyle-BackColor="#000000" BorderColor="#aaaaaa" DayHeaderStyle-BackColor="#5e715e" OtherMonthDayStyle-ForeColor="#cccccc" DayNameFormat="Full" runat="server" TitleStyle-ForeColor="#ffffff" NextPrevStyle-ForeColor="#ffffff" CellSpacing="1" WeekendDayStyle-BackColor="#eeeeee" DayHeaderStyle-ForeColor="#ffffff" SelectionMode="None" TodayDayStyle-BorderColor="#5e715e" TodayDayStyle-BorderWidth="1" TodayDayStyle-Font-Bold="true" TodayDayStyle-ForeColor="#5e715e" /> |
第二步是对内部功能的调整,这个工作主要集中在以下事件的处理上。
代码如下 | 复制代码 |
PreRender:当服务器控件将要呈现给其包含的Page对象时发生。 private void Calendar1_PreRender(object sender, System.EventArgs e) |
例2
前台:
代码如下 | 复制代码 |
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div> <asp:TextBox ID="TextBox1" runat="server" Width="130px"></asp:TextBox> <asp:ImageButton ID="ImageButton1" runat="server" Width="25px" Height="25px" ImageUrl="~/Calender.png" onclick="ImageButton1_Click" /> <br /> <div id="LayerCC" style="left: 167px; width: 220px; position: absolute; top: 40px; height: 191px; background-color: white" visible="false" runat="server"> <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px" onselectionchanged="Calendar1_SelectionChanged"> <DayHeaderStyle BackColor="#66CCFF" Font-Bold="True" Height="1px" /> <DayStyle BackColor="#CCFFFF" /> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" /> <OtherMonthDayStyle ForeColor="#CC9966" /> <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" /> <SelectorStyle BackColor="#FFCC66" /> <TitleStyle BackColor="#0066FF" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" /> <TodayDayStyle BackColor="Red" ForeColor="White" /> </asp:Calendar> </div> </div> </ContentTemplate> </asp:UpdatePanel> |
后台:
代码如下 | 复制代码 |
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { LayerCC.Style["left"] = "167px"; LayerCC.Visible = !LayerCC.Visible; } protected void Calendar1_SelectionChanged(object sender, EventArgs e) |
总结:
(1 采用一些重构,将一些函数方法分离出去,这一块有一些还没分离完全
(2 日期控件还有VisiblMonthChanged事件来处理日历是否被用户更改了月份, e.NewDate.Year 和e.PreviousDate.Year诸如此类的比较
(3 DayRender事件,可以通过cell和Day来呈现日期的特殊性,例如周末和节假日的颜色,
e.Day.IsOtherMOnth e.Day.IsWeekend 等