views:

28

answers:

1

I am adding to a List of DateTime objects on the server every time a date is selected on a Calender control by the client, and in turn highlighting all the selected dates on the control. I'm able to highlight (change back-color) of dates instantiated in the List on page load, and the first selected date by the client. However further date selections on the control are just changing which date is highlighted, ie. not highlighting more.

I had thought by adding the selected DateTime object to a list at runtime upon a selection and then adding each of them to the Calendars "selected dates" property will get around the problem of the calendar control clearing the SelectedDates property on selection of a new date. Debugging by printing all the dates within the dates List to a textbox show that the dates the list was instantiated with and the latest selection are only in the list, not previous selections. My question and what I think is the problem, Can a List on the server be populated by actions from the client at runtime, and added to? I am using ASP with C# .Net3.5 on VS2008. Thanks

My Code System.Collections.Generic.List dates;

    protected void Page_Load(object sender, EventArgs e) {
        this.dates = new List<DateTime>();
        this.dates.Add(new DateTime(2009,12,2));
        this.dates.Add(new DateTime(2009, 12, 3));
        this.dates.Add(new DateTime(2009, 12, 16));
        fillDates();
    }

    protected void AvailCalender_SelectionChanged(object sender, EventArgs e){
        this.dates.Add(this.AvailCalender.SelectedDate);
        foreach (DateTime date in this.dates)
        {
            this.AvailCalender.SelectedDates.Add(date);
            this.displayText.Text = this.displayText.Text + date.ToShortDateString();
        }
        fillDates();
    }


    protected void fillDates()
    {
        foreach (DateTime dates in this.dates)
        {
            this.AvailCalender.SelectedDates.Add(dates);   
        }
        this.AvailCalender.SelectedDayStyle.BackColor = System.Drawing.Color.Blue;
    }
A: 

The List<DateTime> is getting created with each postback so it is not saving the previous selections. You need to persist it in some way like using ViewState, Session or storing it in a DB. Only create it the first time by using Page.IsPostBack to check if this is the first time the page has been hit.

DaveB