取消在ASP.NET Calendar控件日期 [英] Deselect dates in ASP.NET Calendar Control

查看:116
本文介绍了取消在ASP.NET Calendar控件日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择和在C#中的Web日历控件取消选择日期。

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

点击它不会触发选择更改事件,所以Ineed做对的DayRender事件的东西,但我不知道什么或怎么样。

编辑:增加了pre_Render事件code。这似乎是现在的工作,但它似乎有点飘忽不定,例如。
选择日期:OK
选择日期B:好的
取消他们两个:OK
选择日期答:不行的,需要选择两次
取消日期:好的
选择日期C:日期A和C选择

@约翰

是的,我知道该控件是在.NET 2.0框架并没有什么做用C#本身的一部分。

code迄今:

 公共静态列表<&日期时间GT;名单=新名单,LT; D​​ateTime的>();保护无效Calendar1_DayRender(对象发件人,DayRenderEventArgs E)
{
    如果(e.Day.IsSelected ==真)
    {            list.Add(e.Day.Date);
    }
    会话[SelectedDates] =清单;
}保护无效Calendar1_SelectionChanged(对象发件人,EventArgs的发送)
{
    日期时间选择= Calendar1.SelectedDate;    如果(会话[SelectedDates]!= NULL)
    {
        清单<&日期时间GT; newList =(列表<&日期时间GT;)会议[SelectedDates];        的foreach(在newList的DateTime DT)
        {
            Calendar1.SelectedDates.Add(DT);
        }        如果(searchdate(选择,newList))
        {
            Calendar1.SelectedDates.Remove(选择);
        }
        list.Clear();
    }
}公共BOOL searchdate(DateTime的日期,列表与LT; D​​ateTime的>红枣)
{    VAR查询邻的日期=
                其中,o.Date ==日期
                选择o;
        如果(query.ToList()。计数== 0)
        {
            返回false;
        }
        其他
        {
            返回true;
        }    }
   保护无效Calendar1_ preRender(对象发件人,EventArgs的发送)
    {
        如果(Calendar1.SelectedDates.Count == 1)
        {
            的foreach(列表中的DateTime DT)
            {
                如果(searchdate(DT,列表)及&放大器; list.Count == 1)
                {
                    Calendar1.SelectedDates.Clear();                    打破;
                }
            }
        }
    }


解决方案

我一直在寻找今天的快速回答这个问题,但没有找到它,所以我设置为我找到自己的解决方案。我将在这里发布,即使它几乎是一年之后。 (我希望这是不违反规则?)

请注意:我的code是VB,而不是C#

我对这个问题的解决方案是一个布尔变量添加到我的网页类,像这样:

昏暗blnCalendarSelectionChanged由于布尔=假

有了这个,我能够跟踪如果选择已通过添加以下到您calendar_SelectionChanged方法开始改变了:

blnCalendarSelectionChanged = TRUE

布尔是日历SelectionChanged事件已经被解雇后,只有永远正确的。如果仅仅是有一个日期留给被取消它不火SelectionChanged事件。因此,在日历上我有以下的preRender:

 保护小组calShift_ preRender(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手calShift。preRender
    如果blnCalendarSelectionChanged = false,那么
        如果不IsNothing(会话(SelectedDates))然后,
            昏暗newList方式列表(中DATETIME)= CTYPE(会议(SelectedDates),列表(中DATETIME))
            newList.Remove(calShift.SelectedDate)
            会议(SelectedDates)= newList
            calShift.SelectedDate =什么
        万一
    万一
结束小组

要做到这一点在preRender是非常重要的,因为它是在之前的DayRender执行。如果你把这个code中的DayRender,那一天从日历selecteddates删除,但呈现的日历不会更新与此使其显示该日期仍然是选择的用户。

有是,我一直没能找到一种方法,但围绕一个问题。所以如果有当用户导致从另一个控制回发会导致日历失去其选择中选择一个日期的日历preRender上任何控件的回发执行。在我而言这不是一个问题,但我一直在寻找办法解决它的天生丽质的缘故。

这可能不是最好的解决办法,但它为我的作品! :)

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

解决方案

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

这篇关于取消在ASP.NET Calendar控件日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆