Change Visible Month in Calendar

Using .net 4.6.1, I’m doing a calculation on the backend to determine when a customer can select an order date, depending on their weekly invoice day. The calculation is working correctly as is the SelectedDate portion, however, when SelectedDate happens to fall within the following month, my calendar isn’t automatically advancing to show that month by default, it always displays the current month regardless. What am I doing wrong?

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        DayOfWeek invoiceDay = DayOfWeek.Wednesday;
        DateTime nextMonday = GetNextWeekday(DateTime.Today.AddDays(1), DayOfWeek.Monday, invoiceDay);

        if (e.Day.Date < nextMonday)
        {
            e.Day.IsSelectable = false;
            Calendar1.SelectedDate = nextMonday;
            Calendar1.VisibleDate = Calendar1.SelectedDate;
        }
    }

    public static DateTime GetNextWeekday(DateTime start, DayOfWeek day, DayOfWeek _invoiceDay)
    {
        int daysToAdd = ((int)day - (int)start.DayOfWeek + 7) % 7;

        if (_invoiceDay > DateTime.Now.DayOfWeek)
        {
            return start.AddDays(daysToAdd);
        }
        else
        {
            return start.AddDays(daysToAdd + 7);
        }
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I figured out my problem. I just needed to a separate method for the initial calendar setup and call that on page load instead of trying to call it all from the DayRender event. The updated code is below:

    private DayOfWeek logDay = DayOfWeek.Wednesday;
    private DateTime nextMonday;


    protected void Page_Load(object sender, EventArgs e)
    {
        CalendarSetup();
    }

    private void CalendarSetup ()
    {
        if (!IsPostBack)
        {
            logDay = DayOfWeek.Wednesday;
            nextMonday = GetNextWeekday(DateTime.Today.AddDays(1), DayOfWeek.Monday, logDay);
            Calendar1.SelectedDate = nextMonday;
            Calendar1.VisibleDate = Calendar1.SelectedDate;
        }
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date < nextMonday)
        {
            e.Day.IsSelectable = false;
        }
    }

    public static DateTime GetNextWeekday(DateTime start, DayOfWeek day, DayOfWeek _logDay)
    {
        int daysToAdd = ((int)day - (int)start.DayOfWeek + 7) % 7;

        if (_logDay > DateTime.Now.DayOfWeek)
        {
            return start.AddDays(daysToAdd);
        }
        else
        {
            return start.AddDays(daysToAdd + 7);
        }
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x