locked
Can someone help me understand this method RRS feed

  • Question

  • User657329123 posted

    Hi there,

    This code comes from an events calendar. If an event is associated with a date, that date is highlight otherwise it isn't. I'm having a had time understanding this function.

        protected void CalendarDRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
        {
            // If the month is CurrentMonth
            if (!e.Day.IsOtherMonth)
            {
                foreach (DataRow dr in table.Rows)
                {
                    if ((dr["caldate"].ToString() != DBNull.Value.ToString()))
                    {
                        DateTime dtEvent = (DateTime)dr["caldate"];
                        if (dtEvent.Equals(e.Day.Date))
                        {
                            e.Cell.BackColor = Color.Blue;
                            e.Cell.ForeColor = Color.Red;
                        }
                    }
                }
            }
            //If the month is not CurrentMonth then hide the Dates
            else
            {
                e.Cell.Enabled = false;
            }
        }

    I don't understand what is this !e.Day.IsOtherMonth. I have tried running this code in Debug mode to see how control flows but don't understand the above code.

    Joe

    Monday, June 13, 2016 4:41 PM

Answers

  • User2103319870 posted

    I don't understand what is this !e.Day.IsOtherMonth.

    This property is used to check if a current active date is from this month or not.

    FROM MSDN : Gets a value that indicates whether the date represented by an instance of this class is in a month other than the month displayed in the Calendarcontrol

    Complete Code explanation

    protected void CalendarDRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
            {
                //Check if the current active day in calendar is from this month or not
                if (!e.Day.IsOtherMonth)
                {
                    //loop through each rows in datatable
                    foreach (DataRow dr in table.Rows)
                    {
                        //CHeck if caldate column is not equal to null
                        if ((dr["caldate"].ToString() != DBNull.Value.ToString()))
                        {
                            //Conver the caldate value to datetime
                            DateTime dtEvent = (DateTime)dr["caldate"];
                            //check if caldate value is equal to current active date in calendar
                            if (dtEvent.Equals(e.Day.Date))
                            {
                                //If so then hightligh the cell in calendar
                                e.Cell.BackColor = Color.Blue;
                                e.Cell.ForeColor = Color.Red;
                            }
                        }
                    }
                }
                //If the month is not CurrentMonth then hide the Dates
                else
                {
                    e.Cell.Enabled = false;
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 13, 2016 5:29 PM
  • User36583972 posted

    Hi joegreen2005,

    I suggest that you refer to the common usage of this control.

    ASP.NET - Calendars:

    http://www.tutorialspoint.com/asp.net/asp.net_calenders.htm

    Customizing the ASP.NET Calendar Control:

    http://www.codeproject.com/Articles/229445/Customizing-the-ASP-NET-Calendar-Control

    Cool Calendar - Amazing Calendar without popup:

    http://www.codeproject.com/Articles/8535/Cool-Calendar-Amazing-Calendar-without-popup

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 14, 2016 7:05 AM

All replies

  • User2103319870 posted

    I don't understand what is this !e.Day.IsOtherMonth.

    This property is used to check if a current active date is from this month or not.

    FROM MSDN : Gets a value that indicates whether the date represented by an instance of this class is in a month other than the month displayed in the Calendarcontrol

    Complete Code explanation

    protected void CalendarDRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
            {
                //Check if the current active day in calendar is from this month or not
                if (!e.Day.IsOtherMonth)
                {
                    //loop through each rows in datatable
                    foreach (DataRow dr in table.Rows)
                    {
                        //CHeck if caldate column is not equal to null
                        if ((dr["caldate"].ToString() != DBNull.Value.ToString()))
                        {
                            //Conver the caldate value to datetime
                            DateTime dtEvent = (DateTime)dr["caldate"];
                            //check if caldate value is equal to current active date in calendar
                            if (dtEvent.Equals(e.Day.Date))
                            {
                                //If so then hightligh the cell in calendar
                                e.Cell.BackColor = Color.Blue;
                                e.Cell.ForeColor = Color.Red;
                            }
                        }
                    }
                }
                //If the month is not CurrentMonth then hide the Dates
                else
                {
                    e.Cell.Enabled = false;
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 13, 2016 5:29 PM
  • User657329123 posted

    Thanks A2H. If I comment out the following lines and say if I'm in the month of May then I no longer see dates highlighted even if an even is associated with that date.  But if I don't comment it out, then I see the event details associated with a date in May but the calendar displays the current month instead of May. Any idea why?

        protected void CalendarDRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
        {
            // If the month is CurrentMonth
            //if (!e.Day.IsOtherMonth)
            //{
                foreach (DataRow dr in table.Rows)
                {
                    if ((dr["caldate"].ToString() != DBNull.Value.ToString()))
                    {
                        DateTime dtEvent = (DateTime)dr["caldate"];
                        if (dtEvent.Equals(e.Day.Date))
                        {
                            e.Cell.BackColor = Color.Aquamarine;
                            e.Cell.ForeColor = Color.Crimson;
                        }
                    }
                }
            //}
            //If the month is not CurrentMonth then hide the Dates
            //else
            //{
            //    e.Cell.Enabled = false;
            //}
        }

    I'm lost.

    Monday, June 13, 2016 6:07 PM
  • User36583972 posted

    Hi joegreen2005,

    I suggest that you refer to the common usage of this control.

    ASP.NET - Calendars:

    http://www.tutorialspoint.com/asp.net/asp.net_calenders.htm

    Customizing the ASP.NET Calendar Control:

    http://www.codeproject.com/Articles/229445/Customizing-the-ASP-NET-Calendar-Control

    Cool Calendar - Amazing Calendar without popup:

    http://www.codeproject.com/Articles/8535/Cool-Calendar-Amazing-Calendar-without-popup

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 14, 2016 7:05 AM