1

I'm using Calendar controller in my asp.net web forms application. I followed this article to implement Calendar in my application. I'm adding selected days into a List<DateTime> to remember selected dates and use them in future actions.

Now I have added buttons to my page like Select Weekends, Select Weekdays, Select Month and Select Year.

  • if I click on the Select Weekends button, I need to select all weekend days of current month and add them into List<DateTime>.

  • if I click on the Select Weekdays button, I need to select all week days of current month and add them into List<DateTime>.

  • if I click on the Select Month** button, I need to select all days of the current month and add them into List<DateTime>.

  • if I click on the Select Year button, I need to select all days of the current year and add them into List<DateTime>.

How can I do this programatically using C#?

1 Answer 1

2

I don't think there's a miracle solution, here how I would write 2 methods to what you want for the weekend days. For the other points, you could do more or less the same thing:

    protected void WeekendDays_Button_Click(object sender, EventArgs e)
    {
        this.SelectWeekEnds():
    }

    private void SelectWeekEnds(){
        //If you need to get the selected date from calendar
        //DateTime dt = this.Calendar1.SelectedDate;

        //If you need to get the current date from today
        DateTime dt = DateTime.Now;

        List<DateTime> weekendDays = this.SelectedWeekEnds(dt);
        weekendDays.ForEach(d => this.Calendar1.SelectedDates.Add(d));
    }

    private List<DateTime> GetWeekEndDays(DateTime DT){
        List<DateTime> result = new List<DateTime>();
        int month = DT.Month;
        DT = DT.AddDays(-DT.Day+1);//Sets DT to first day of month

        //Sets DT to the first week-end day of the month;
        if(DT.DayOfWeek != DayOfWeek.Sunday)
            while (DT.DayOfWeek != DayOfWeek.Saturday)
                DT = DT.AddDays(1);

        //Adds the week-end day and stops when next month is reached.
        while (DT.Month == month)
        {
            result.Add(DT);
            DT = DT.AddDays(DT.DayOfWeek == DayOfWeek.Saturday ? 1 : 6);
        }
        return result;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

I need to change back color of selected dates. how can i do this ?
Example: this.Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.Red;
can you explain this line please ? DT = DT.AddDays(DT.DayOfWeek == DayOfWeek.Saturday ? 1 : 6);. i'm trying to get week days. but still unable to do it correctly.
If guess you may have a look at the ternary operator definition: msdn.microsoft.com/en-us/library/ty67wk28%28v=vs.80%29.aspx If DT.DayOfWeek is Saturday, this line increments DT by 1 day, if DT.DayOfWeek is Sunday, it adds 6 days to get to the next Saturday. I did not write a complete solution for your 4 different points, I just wanted to show a way to go, you may have to do consequent changes in the logic to write the other methods (I mean not just copy/paste and change a few things).
I have modified your code to get week days. but it is not working properly. i have added it as question . can you look at it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.