Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2700
Initialize WebMonthCalendar
posted

I have added a WebmonthCalendar to my page and set it to have

SelectionType = "Week"

Now I want to set the initial date and have set this server side using

WebMonthCalendar1.SelectedDate = DateTime.Now

I sort of expected it to select the current week - but it doesn't.  If I click to select a day within the week the week itself is selected but I need to set it initially to select the current week.  How do I do this?

Parents
No Data
Reply
  • 19693
    Suggested Answer
    posted

    Hello cmdrew ,

    On the server you should specify RangeMin and RangeMax of the SelectedDates collection:

    protected void Page_Load(object sender, EventArgs e)

        {      

            DateTime dt = DateTime.Now;

            DateTime startDate = DateTimeHelper.StartOfWeek(dt, DayOfWeek.Monday);

            DateTime endDate = DateTimeHelper.EndOfWeek(dt, DayOfWeek.Sunday);      

            WebMonthCalendar1.SelectedDates.RangeMin = startDate;

            WebMonthCalendar1.SelectedDates.RangeMax = endDate;        

        }

     

     

    public static class DateTimeHelper

    {

        public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)

        {

            int diff = dt.DayOfWeek - startOfWeek;

            if (diff < 0)

            {

                diff += 7;

            }

            return dt.AddDays(-1 * diff).Date;

        }

     

        public static DateTime EndOfWeek(this DateTime dt, DayOfWeek endOfWeek)

        {

            int diff = endOfWeek - dt.DayOfWeek;

            if (diff < 0)

            {

                diff += 7;

            }

            return dt.AddDays(diff).Date;

        }

    }

    Please let me know if you need further assistance regarding this

Children