我有asp日历来选择多个日期。
<asp:UpdatePanel ID="updpnl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" NextPrevFormat="FullMonth" OnPreRender="Calendar1_PreRender" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender">
</asp:Calendar>
<asp:Button ID="btnClearSelection" runat="server" Text="Clear"
OnClick="btnClearSelection_Click" />
<br />
</ContentTemplate>
</asp:UpdatePanel>
代码如下:
protected void Page_Load(object sender, EventArgs e)
{
}
public List<DateTime> SelectedDates
{
get
{
if (ViewState["Dates"] == null)
ViewState["Dates"] = new List<DateTime>();
return (List<DateTime>)ViewState["Dates"];
}
set
{
ViewState["Dates"] = value;
}
}
protected void Calendar1_PreRender(object sender, EventArgs e)
{
// Reset Selected Dates
Calendar1.SelectedDates.Clear();
// Select previously Selected Dates
foreach (DateTime dt in SelectedDates)
Calendar1.SelectedDates.Add(dt);
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
//Check if selected Date is in the saved list
// Remove the Selected Date from the saved list
if (SelectedDates.Contains(Calendar1.SelectedDate))
SelectedDates.Remove(Calendar1.SelectedDate);
else
SelectedDates.Add(Calendar1.SelectedDate);
ViewState["Dates"] = SelectedDates;
}
protected void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
if (e.Day.Date <= DateTime.Today)
{
e.Day.IsSelectable = false;
}
// Disable dates of past/future months
if (e.Day.IsOtherMonth)
{
e.Day.IsSelectable = false;
e.Cell.Text = "X";
}
}
}
如果我不选择任何日期并尝试提交,则在Calendar1.SelectedDates.Count中显示“1”,值为“{12/29/9999}”。 如何从List中删除它?
谢谢。
答案 0 :(得分:1)
您可以尝试选择多个日期:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.IsSelected == true)
{
list.Add(e.Day.Date);
}
Session["SelectedDates"] = list;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
if (Session["SelectedDates"] != null)
{
List<DateTime> newList = (List<DateTime>)Session["SelectedDates"];
foreach (DateTime dt in newList)
{
Calendar1.SelectedDates.Add(dt);
}
list.Clear();
}
}