Deselect dates in ASP.NET Calendar Control

Posted by yomismo on Stack Overflow See other posts from Stack Overflow or by yomismo
Published on 2010-04-13T07:29:23Z Indexed on 2010/04/13 8:12 UTC
Read the original article Hit count: 325

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about calendar

Related posts about c#