Code
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999"
CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="Black" Height="180px" Width="200px">
<SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
<SelectorStyle BackColor="#CCCCCC" />
<WeekendDayStyle BackColor="#FFFFCC" />
<TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
<OtherMonthDayStyle ForeColor="#808080" />
<NextPrevStyle VerticalAlign="Bottom" />
<DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
</asp:Calendar>
选择时间:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">Today</asp:ListItem>
<asp:ListItem Value="-1">yesterday</asp:ListItem>
<asp:ListItem Value="1">tomorrow</asp:ListItem>
</asp:DropDownList>
html代码,注意的是DorpDownList要把AutoPostBack设为True
下面是CS代码
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.Calendar1.SelectedDayStyle.BackColor = Color.Red;
this.Calendar1.SelectedDate = DateTime.Today;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Calendar1.SelectedDate = DateTime.Today.AddDays(Convert.ToInt32(this.DropDownList1.SelectedValue));
}
这个业务逻辑很奇怪! 已经选择了还要再点一下,还不如放个文本框,直接获取dropdownlist的SelectedValue
不过,学习是可以的。其实很简单
DateTime tomorrow = DateTime.Today.AddDays(1);
Calendar1.TodaysDate = tomorrow;
Calendar1.SelectedDate = Calendar1.TodaysDate;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DateTime today = System.DateTime.Today;
DateTime yesterday = today.AddDays(-1);
DateTime tomorrow = today.AddDays(1);
DropDownList1.Items.Add(String.Format("{0:dd MMM yyyy}",
today));
DropDownList1.Items.Add(String.Format("{0:dd MMM yyyy}",
yesterday));
DropDownList1.Items.Add(String.Format("{0:dd MMM yyyy}",
tomorrow));
}
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
Calendar1.TodaysDate =
DateTime.Parse(DropDownList1.SelectedItem.Text);
}
http://msdn.microsoft.com/zh-cn/library/8k0f6h1h.aspx