Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.Net Multi-day event Calendar Functionality

Posted on 2009-03-30
3
Medium Priority
?
435 Views
Last Modified: 2013-11-26
I had found this link: http://forums.asp.net/t/959533.aspx and it explains how to modify your current calendar code to allow for multi-day events. This would be terrific if the link wasn't broken for the download. So I ask, using this code, how would I go about creating this calendar? I know there are plenty out there you can buy, looking to avoid that. I have also included the code listed on the forum below, in case you don't want to click the link.
This is a C# dll project, so you'll need vs standard or C# express sku to open it. In the EventCalendar.cs code:
 
a) Add the following property. It specifies the name of the field representing the end date:
 
        [DefaultValue("")]
        [Themeable(false)]
        public virtual string EndDayField
        {
            get
            {
                object o = ViewState["EndDayField"];
                if (o != null)
                {
                    return (string)o;
                }
                return String.Empty;
            }
            set
            {
                ViewState["EndDayField"] = value;
                if (Initialized)
                {
                    OnDataPropertyChanged();
                }
            }
        }
 
b) Replace the existing versions of the following methods:
 
       private int CreateDataBoundChildren(System.Data.DataView dv, Table table, DateTime todaysDate, DateTime visibleDate, System.Globalization.Calendar threadCalendar)
        {
            DateTime firstDay = FirstCalendarDay(this.VisibleDate);
            dv.Table.Locale = new CultureInfo("en-US");
 
            int dayoffset = 0;
            string dayField = this.DayField;
            string endDayField = this.EndDayField;
 
            for (int iRow = 0; iRow < 6; iRow++)
            {
                TableRow row = new TableRow();
                table.Rows.Add(row);
 
                for (int iDay = 0; iDay < 7; iDay++)
                {
                    DateTime d = firstDay.AddDays(dayoffset);
 
                    //Initialize the cell
                    CalendarDay day = getDay(d, TodaysDate, visibleDate, threadCalendar);
                    TableCell cell = CreateDayCell(day);
                    row.Cells.Add(cell);
 
                    //Process real data for this day
                    dv.RowFilter = string.Format("IsNull({1},{0}) >= #{2}# AND {0}<#{3}#", dayField, endDayField, d.ToString("MM/dd/yyyy"), d.AddDays(1).ToString("MM/dd/yyyy"));
               
                    if (dv.Count > 0 && this.DayEventTemplate != null)
                    {
                        foreach (System.Data.DataRowView drv in dv)
                        {
                            DataBoundCalendarItem dataitem = new DataBoundCalendarItem(drv, day);
                            DayEventTemplate.InstantiateIn(dataitem);
                            //add the controls to both collections
                            cell.Controls.Add(dataitem);
                            //databind the data item
                            dataitem.DataBind();
                        }
                    }
                    else if (this.DayEmptyTemplate != null)
                    {
                        DataBoundCalendarItem dataitem = new DataBoundCalendarItem(null, day);
                       DayEmptyTemplate.InstantiateIn(dataitem);
                        //add the controls to both collections
                        cell.Controls.Add(dataitem);
 
                    }
                    dayoffset++;
                }
            }
            return 1;
        }
 
and
 
    void cacheDataInViewstate(System.Data.DataView dv)
        {
            DateTime firstDay = FirstCalendarDay(this.VisibleDate);
            DateTime lastDay = EndDate(this.VisibleDate);
 
            System.Collections.Generic.Dictionary<DateTime, int> ctrlcount = new System.Collections.Generic.Dictionary<DateTime, int>();
 
            foreach (System.Data.DataRowView drv in dv)
            {              
                DateTime startdate = ((DateTime)drv[DayField]).Date;
                DateTime enddate = (drv[EndDayField] != DBNull.Value) ? ((DateTime)drv[EndDayField]).Date : startdate;
                DateTime rowdate = startdate;
                while (rowdate <= enddate)
                {
                    if (rowdate >= firstDay && rowdate <= lastDay)
                    {
                        if (ctrlcount.ContainsKey(rowdate))
                        {
                            ctrlcount[rowdate] += 1;
                        }
                        else
                        {
                            ctrlcount[rowdate] = 1;
                        }
                    }
                    rowdate = rowdate.AddDays(1);
                }
            }
            ViewState[ViewStateDataKey] = ctrlcount;
        }
 
 
That should be all you need in the calendar. Build it and either reference it from the website, or copy the output to the bin directory.
 
The eventscalendar.aspx page needs to be modified to query for the end data as part of the sql datasource and to set the name of the enddate field on the calendar control. For example:
 
<asp:SqlDataSource id=SqlDataSource1 SelectCommand="SELECT [id], [starttime], [endtime], [title], [description] FROM [Events]" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>" runat="server"></asp:SqlDataSource>
            <ec:EventCalendar id=eventscalendar CssClass="eventmonthtable" runat="server" ShowTitle="true" EndDayField="endtime" DayField="starttime" BorderWidth="0" DataSourceID="sqldatasource1">
...
</ec:EventCalendar>
 
That should be all that you need to have long running events show up on each day that they occur.

Open in new window

0
Comment
Question by:GLIanimal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
ITHelper80 earned 2000 total points
ID: 24025665
You can download the file here, last post.

http://forums.asp.net/p/888804/938287.aspx#938287 
0
 

Author Comment

by:GLIanimal
ID: 24027484
Thanks, guess I had a case of the Mondays yesterday lol
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24027506
NP The page your posted did have a broken link, I just found a different one =)
0

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question