C# ASPX日历保留会话[“选定日期”]
我有一个日历,可以在ASP中选择多个日期。问题是,在切换到其他月份时,会话[“SelectedDates”]将为空(可能是因为之前的日期不在视图中)。这防止我从多个月中选择多个日期,因为其他月份的日期将被清除。我如何解决这个问题 我一直在尝试在OnVisibleMonChanged期间将日期存储在临时变量中,但我无法使代码正常工作C# ASPX日历保留会话[“选定日期”],c#,asp.net,C#,Asp.net,我有一个日历,可以在ASP中选择多个日期。问题是,在切换到其他月份时,会话[“SelectedDates”]将为空(可能是因为之前的日期不在视图中)。这防止我从多个月中选择多个日期,因为其他月份的日期将被清除。我如何解决这个问题 我一直在尝试在OnVisibleMonChanged期间将日期存储在临时变量中,但我无法使代码正常工作 private List<DateTime> datelist = new List<DateTime>(); protected void
private List<DateTime> datelist = new List<DateTime>();
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
if (Session["SelectedDates"] != null)
{
List<DateTime> newList = (List<DateTime>)Session["SelectedDates"];
foreach (DateTime dt in newList)
{
if (Calendar1.SelectedDates.Contains(dt))
{
Calendar1.SelectedDates.Remove(dt);
}
else
{
Calendar1.SelectedDates.Add(dt);
}
}
datelist.Clear();
}
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.IsSelected == true)
{
datelist.Add(e.Day.Date);
}
Session["SelectedDates"] = datelist;
}
private List datelist=new List();
受保护的无效日历1\u选择已更改(对象发件人、事件参数e)
{
如果(会话[“SelectedDates”]!=null)
{
列表新建列表=(列表)会话[“选定日期”];
foreach(newList中的DateTime dt)
{
如果(日历1.选定日期.包含(dt))
{
日历1.选定日期。删除(dt);
}
其他的
{
日历1.选定日期。添加(dt);
}
}
datelist.Clear();
}
}
受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e)
{
如果(e.Day.IsSelected==true)
{
日期列表。添加(例如日期);
}
会话[“选定日期”]=日期列表;
}
ASPX
您的日历中似乎没有很好地处理回发,因为当您选择日历中的任何日期时,会调用Calendar1\u DayRender,并将日期列表设置为null,因此会话将填充null,因此,在Calendar1\u SelectionChanged中,会话结果将返回NULL。您可以通过在Calendar1\u DayRender函数上添加断点进行检查,并监视其操作 在您的日历中,回发似乎没有得到很好的处理,因为当您选择日历中的任何日期时,会调用Calendar1\u DayRender,并将日期列表设置为null,因此会话将填充null,因此,在Calendar1\u SelectionChanged中,会话结果将返回NULL。您可以通过在Calendar1\u DayRender函数上添加断点进行检查,并监视其操作 试试看,跨月选择多个日期是可行的。确保两个事件都已链接
public static List<DateTime> dateList = new List<DateTime>();
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.IsSelected == true)
{
dateList.Add(e.Day.Date);
}
Session["SelectedDates"] = dateList;
}
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);
}
dateList.Clear();
}
}
publicstaticlist dateList=newlist();
受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e)
{
如果(e.Day.IsSelected==true)
{
日期列表。添加(例如日期);
}
会话[“选定日期”]=日期列表;
}
受保护的无效日历1\u选择已更改(对象发件人、事件参数e)
{
如果(会话[“SelectedDates”]!=null)
{
列表新建列表=(列表)会话[“选定日期”];
foreach(newList中的DateTime dt)
{
日历1.选定日期。添加(dt);
}
dateList.Clear();
}
}
试试看,跨月选择多个日期是可行的。确保两个事件都已链接
public static List<DateTime> dateList = new List<DateTime>();
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.IsSelected == true)
{
dateList.Add(e.Day.Date);
}
Session["SelectedDates"] = dateList;
}
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);
}
dateList.Clear();
}
}
publicstaticlist dateList=newlist();
受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e)
{
如果(e.Day.IsSelected==true)
{
日期列表。添加(例如日期);
}
会话[“选定日期”]=日期列表;
}
受保护的无效日历1\u选择已更改(对象发件人、事件参数e)
{
如果(会话[“SelectedDates”]!=null)
{
列表新建列表=(列表)会话[“选定日期”];
foreach(newList中的DateTime dt)
{
日历1.选定日期。添加(dt);
}
dateList.Clear();
}
}
我尝试过类似的方法,它可能很有用:
protected void C1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = "";
L1.Items.Add(C1.SelectedDate.ToShortDateString());
ListItem[] x = new ListItem[L1.Items.Count];
L1.Items.CopyTo(x, 0);
Session["x"] = x;
ListItem[] collection = (ListItem[])Session["x"];
foreach (var item in collection)
{
Label1.Text += item.Text + "</br>";
}
}
protectedvoid C1\u SelectionChanged(对象发送方,事件参数e)
{
标签1.Text=“”;
L1.Items.Add(C1.SelectedDate.ToSortDateString());
ListItem[]x=新的ListItem[L1.Items.Count];
L1.项目。复制到(x,0);
会话[“x”]=x;
ListItem[]集合=(ListItem[])会话[“x”];
foreach(集合中的var项)
{
Label1.Text+=item.Text+“”;
}
}
你做这件事的方式有:
List<DateTime> lb = new List<DateTime>();
lb.Add(C1.SelectedDate);
DateTime[] a = lb.ToArray();
lb.CopyTo(a, 0);
Session["x"] = a;
DateTime[] collection = (DateTime[])Session["x"];
foreach (var item in collection)
{
Label1.Text += item.ToShortDateString() + "</br>";
}
List lb=新列表();
lb.添加(C1.所选日期);
DateTime[]a=lb.ToArray();
lb.CopyTo(a,0);
会话[“x”]=a;
DateTime[]集合=(DateTime[])会话[“x”];
foreach(集合中的var项)
{
Label1.Text+=item.toSortDateString()+“”;
}
我尝试过类似的方法,它可能很有用:
protected void C1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = "";
L1.Items.Add(C1.SelectedDate.ToShortDateString());
ListItem[] x = new ListItem[L1.Items.Count];
L1.Items.CopyTo(x, 0);
Session["x"] = x;
ListItem[] collection = (ListItem[])Session["x"];
foreach (var item in collection)
{
Label1.Text += item.Text + "</br>";
}
}
protectedvoid C1\u SelectionChanged(对象发送方,事件参数e)
{
标签1.Text=“”;
L1.Items.Add(C1.SelectedDate.ToSortDateString());
ListItem[]x=新的ListItem[L1.Items.Count];
L1.项目。复制到(x,0);
会话[“x”]=x;
ListItem[]集合=(ListItem[])会话[“x”];
foreach(集合中的var项)
{
Label1.Text+=item.Text+“”;
}
}
你做这件事的方式有:
List<DateTime> lb = new List<DateTime>();
lb.Add(C1.SelectedDate);
DateTime[] a = lb.ToArray();
lb.CopyTo(a, 0);
Session["x"] = a;
DateTime[] collection = (DateTime[])Session["x"];
foreach (var item in collection)
{
Label1.Text += item.ToShortDateString() + "</br>";
}
List lb=新列表();
lb.添加(C1.所选日期);
DateTime[]a=lb.ToArray();
lb.CopyTo(a,0);
会话[“x”]=a;
DateTime[]集合=(DateTime[])会话[“x”];
foreach(集合中的var项)
{
Label1.Text+=item.toSortDateString()+“”;
}
我发现处理此问题的更好方法是使用ViewState。我修改了源代码。每当更改选择时,我都会将ViewState传输到会话。然后可以从会话中获取日期
public List<DateTime> SelectedDates
{
get
{
if (ViewState["Dates"] != null)
return (List<DateTime>)ViewState["Dates"];
else
return new List<DateTime>() { Calendar1.SelectedDate };
}
set
{
ViewState["Dates"] = value;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
foreach (DateTime selectedDate in Calendar1.SelectedDates)
{
if (SelectedDates.Contains(selectedDate))
SelectedDates.Remove(selectedDate);
else
SelectedDates.Add(selectedDate);
}
ViewState["Dates"] = SelectedDates;
Session["SelectedDates"] = ViewState["Dates"];
}
protected void Calendar1_PreRender(object sender, EventArgs e)
{
Calendar1.SelectedDates.Clear();
foreach (DateTime dt in SelectedDates)
Calendar1.SelectedDates.Add(dt);
}
公共列表选定日期
{
得到
{
如果(ViewState[“Dates”]!=null)
返回(列表)视图状态[“日期