简体   繁体   English

在ASP.NET日历控件中取消选择日期

[英]Deselect dates in ASP.NET Calendar Control

I'm trying to select and de-select dates on a C# Web Calendar control. 我正在尝试选择和取消选择C#Web日历控件上的日期。

The problem I have is that I can select or deselect dates except when there is only a single date selected. 我的问题是我可以选择或取消选择日期,除非只有一个选择的日期。

Clicking on it does not trigger the selection changed event, so Ineed to do something on the dayrender event but I'm not sure what or how. 单击它不会触发选择更改事件,因此Ineed可以在dayrender事件中执行某些操作,但是我不确定是什么或如何执行。

Edit: Added the Pre_Render event code. 编辑:添加了Pre_Render事件代码。 This seems to work now, however it seems a little bit erratic,eg select date A : OK Select date B :OK deselect them both: OK select date A: Does not work, need to select it twice deselect date A : Ok Select Date C: dates A and c are selected 这现在似乎可以正常工作,但是似乎有点不稳定,例如,选择日期A:确定选择日期B:确定同时取消选择它们:确定选择日期A:不起作用,需要选择两次以取消选择日期A:确定选择日期C:选择日期A和c

@John @约翰

Yes, I am aware that the control is part of the .NET 2.0 framework and nothing to do with C# per se. 是的,我知道控件是.NET 2.0框架的一部分,与C#本身无关。

Code so far: 到目前为止的代码:

public static List<DateTime> list = new List<DateTime>();

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)
{
    DateTime selection = Calendar1.SelectedDate;

    if (Session["SelectedDates"] != null)
    {
        List<DateTime> newList = (List<DateTime>)Session["SelectedDates"];

        foreach (DateTime dt in newList)
        {
            Calendar1.SelectedDates.Add(dt);
        }

        if (searchdate(selection, newList))
        {
            Calendar1.SelectedDates.Remove(selection);
        }


        list.Clear();
    }
}

public bool searchdate(DateTime date, List<DateTime> dates)
{

    var query = from o in dates
                where o.Date == date
                select o;
        if (query.ToList().Count == 0)
        {
            return false;
        }
        else
        {
            return true;
        }

    }


   protected void Calendar1_PreRender(object sender, EventArgs e)
    {
        if (Calendar1.SelectedDates.Count == 1)
        {
            foreach (DateTime dt in list)
            {
                if (searchdate(dt, list) && list.Count == 1)
                {


                    Calendar1.SelectedDates.Clear();

                    break;
                }
            }
        }
    }

I was looking for a quick answer to this problem today but could not find it so I set to find my own solution. 我今天一直在寻找这个问题的快速答案,但找不到它,所以我开始寻找自己的解决方案。 I will post it here even if it is almost a year later. 即使将近一年,我也会在这里发布。 (I hope that isn't against the rules?) (我希望这不违反规则吗?)

Note: My code was in VB and not C# 注意:我的代码是在VB中而不是C#中

My solution to this problem was to add a boolean variable to my page class like so: 我对这个问题的解决方案是向我的页面类添加一个布尔变量,如下所示:

Dim blnCalendarSelectionChanged As Boolean = False 昏暗blnCalendarSelectionChanged为Boolean = False

With this I am able to keep track if the selection has changed by adding the following to the start of your calendar_SelectionChanged method: 有了这个,我可以通过将以下内容添加到calendar_SelectionChanged方法的开头来跟踪选择是否已更改:

blnCalendarSelectionChanged = True blnCalendarSelectionChanged = True

The boolean is only ever true after the calendars SelectionChanged event has been fired. 仅在触发日历SelectionChanged事件后,布尔值才为true。 When there is only a single date left to be deselected it does not fire the SelectionChanged event. 如果只剩下一个要取消选择的日期,则不会触发SelectionChanged事件。 So on the PreRender of the calendar I have the following: 因此,在日历的PreRender上,我有以下内容:

Protected Sub calShift_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles calShift.PreRender
    If blnCalendarSelectionChanged = False Then
        If Not IsNothing(Session("SelectedDates")) Then
            Dim newList As List(Of DateTime) = CType(Session("SelectedDates"), List(Of DateTime))
            newList.Remove(calShift.SelectedDate)
            Session("SelectedDates") = newList
            calShift.SelectedDate = Nothing
        End If
    End If
End Sub

It is important to do this in the PreRender because it is executed before the DayRender. 在PreRender中执行此操作很重要,因为它在DayRender之前执行。 If you put this code in the DayRender then the day is removed from the calendars selecteddates BUT the calendars render is not updated with this making it appear to the user that the date is still selected. 如果将这段代码放在DayRender中,则日期将从选定的日历中删除,但是日历渲染不会随之更新,这使用户似乎仍选择了日期。

There is a problem that I haven't been able to find a way around yet. 我还没有找到解决问题的方法。 The calendars PreRender is executed on the postback of any control so if there is a single date selected when the user causes a postback from another control it will cause the calendar to lose its selection. 日历PreRender是在任何控件的回发上执行的,因此,当用户从另一个控件进行回发时,如果选择单个日期,则日历将丢失其选择。 In my case this is not a problem but I have been looking for a way around it for perfections sake. 就我而言,这不是问题,但为了完美起见,我一直在寻找解决方法。

This may not be the best solution but it works for me! 这可能不是最佳解决方案,但对我有用! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM