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

发布于 2024-08-28 19:46:49 字数 1880 浏览 0 评论 0原文

我正在尝试在 C# Web 日历控件上选择和取消选择日期。

我遇到的问题是,我可以选择或取消选择日期,除非仅选择一个日期。

单击它不会触发选择更改事件,因此我需要在 dayrender 事件上做一些事情,但我不确定做什么或如何做。

编辑:添加了 Pre_Render 事件代码。这现在似乎有效,但似乎有点不稳定,例如 选择日期A:确定 选择日期B:确定 取消选择它们:确定 选择日期 A: 不起作用,需要选择两次 取消选择日期 A : 好的 选择日期 C:选择日期 A 和 c

@John

是的,我知道该控件是 .NET 2.0 框架的一部分,与 C# 本身无关。

到目前为止的代码:

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'm trying to select and de-select dates on a C# Web Calendar control.

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.

Edit: Added the Pre_Render event code. This seems to work now, however it seems a little bit erratic,e.g.
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

@John

Yes, I am aware that the control is part of the .NET 2.0 framework and nothing to do with C# per se.

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;
                }
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2024-09-04 19:46:49

我今天正在寻找这个问题的快速答案,但找不到它,所以我开始寻找自己的解决方案。即使快一年后了,我也会把它贴在这里。 (我希望这不违反规则?)

注意:我的代码是 VB 而不是 C#

我对此问题的解决方案是向我的页面类添加一个布尔变量,如下所示:

Dim blnCalendarSelectionChanged As Boolean = False

有了这个我通过将以下内容添加到 calendar_SelectionChanged 方法的开头,可以跟踪选择是否已更改:

blnCalendarSelectionChanged = True

该布尔值仅在触发 calendars SelectionChanged 事件后才为 true。当只剩下一个日期需要取消选择时,它不会触发 SelectionChanged 事件。因此,在日历的 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

在 PreRender 中执行此操作很重要,因为它是在 DayRender 之前执行的。如果您将此代码放入 DayRender 中,则该日期将从日历 selecteddates 中删除,但日历渲染不会随之更新,从而使用户看起来该日期仍处于选中状态。

有一个问题我还没有找到解决方法。日历 PreRender 在任何控件的回发时执行,因此,如果当用户从另一个控件引发回发时选择了单个日期,则会导致日历丢失其选择。就我而言,这不是问题,但为了完美,我一直在寻找解决方法。

这可能不是最好的解决方案,但它对我有用! :)

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#

My solution to this problem was to add a boolean variable to my page class like so:

Dim blnCalendarSelectionChanged As 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:

blnCalendarSelectionChanged = True

The boolean is only ever true after the calendars SelectionChanged event has been fired. When there is only a single date left to be deselected it does not fire the SelectionChanged event. So on the PreRender of the calendar I have the following:

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. 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.

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. 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! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文