CMSCalendar month update

joyanta sen asked on July 13, 2018 23:18

HI, I have a requirement to change the month of the calendar based on the querystring value. For ex, if the month value(passing as parameter) in querystring is "January", the calendar should display me the January month. Could you please tell me how to achieve that?

Thanks, Joyanta

Correct Answer

Dragoljub Ilic answered on July 16, 2018 17:10

Hi Joyanta,

Code should be placed in SetupControl, under OnContentLoaded event in CMSCalendar (your cloned file).

Did you get any errors, maybe to check event log? Is something breaks, or simply don't work? On which version of Kentico you tried this?

Can you please try to add this line of code as well (for VisibleDate):

var month = ValidationHelper.GetInteger(QueryHelper.GetString("month", ""), DateTime.Now.Month);
var date = new DateTime(DateTime.Now.Year, month, 1);
calItems.TodaysDate = TimeZoneMethods.ConvertDateTime(date, this);
calItems.VisibleDate = TimeZoneMethods.ConvertDateTime(date, this);

Best regards, Dragoljub

1 votesVote for this answer Unmark Correct answer

Recent Answers


Dragoljub Ilic answered on July 16, 2018 10:54 (last edited on July 16, 2018 10:59)

Hi Joyanta,

I'm not sure that is supported out of the box in Kentico, but with simple code changes, you can modify it easily. You should take a look into SetupControl method in cms.calendar.ascx.cs. You can find it on this path: CMSWebParts/Viewers/Documents/cmscalendar.ascx. There is a line of code where is as default value assigned current date:

calItems.TodaysDate = TimeZoneMethods.ConvertDateTime(DateTime.Now, this);

You should change that to something like this:

var month = ValidationHelper.GetInteger(QueryHelper.GetString("month", ""), DateTime.Now.Month);
var date = new DateTime(DateTime.Now.Year, month, 1);
calItems.TodaysDate = TimeZoneMethods.ConvertDateTime(date, this);

This will select month passed from url in current year. Url should look like /some-page-name?month=1

You can use this to convert month name to int (check this post), but handle it properly for correct values: int month = DateTime.ParseExact("January", "MMMM", CultureInfo.CurrentCulture ).Month

NOTE: It's not desirable to make changes on kentico's files, because next upgrade could replace your changes. You should clone 'Calendar' web part and make changes on custom file.

Best regards, Dragoljub

1 votesVote for this answer Mark as a Correct answer

joyanta sen answered on July 16, 2018 16:21

Hi Dragoljub Ilic, Thanks for your response. Could you please tell me this code to be under which event handler. I have cloned the CMSCalendar. I tried to put the code inside SetupControl() or DayRender() but didn't work.

Thanks, Joyanta

0 votesVote for this answer Mark as a Correct answer

joyanta sen answered on July 16, 2018 20:44

Thanks a lot Dragoljub. It worked. I think VisibleDate make it work.

Thanks, Joanta

0 votesVote for this answer Mark as a Correct answer

joyanta sen answered on July 17, 2018 19:58

Hi Dragoljub Ilic,

Could you please tell me how to access the events inside the calendar cell from code behind, since I need to wrap all the events in a particular style.

Thanks.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.