简体   繁体   中英

Deselect dates in ASP.NET Calendar Control

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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