So I had some trouble finding a good way on how to create a recurring event in asp.net. After playing around for a bit this is the concept I came up with and it worked quite well. The biggest date range I tested was about 12 months worth. I was worried about how many iterations this concept would use, but turned out it wasn't that bad.
You figure the average user will have a recurring event 2-3 days a week and at the most 12 months worth. With the method below it would create about a total of 1090 iterations. This would cause for about 145 DB entries (if you store each event as a seperate record). Their are different ways to store the events, but I am doing a seperate record right now just because I don't want to have to touch the backend of the DB I'm working with (yes I know poor excuse).
Their isn't any performance hit for this so far. Then again I don't have it writing to the DB yet either. So with that said if anyone else can come up with a more efficient concept please let me know.
Concept
Have user choose which days of the week the event is on
Have user choose time of the event
Have user choose a date range for the events recurrence
Store event information in a struct
Store the struct in an array
Store the days of the week of the event in an array
this lets us avoid having to keep iterating the checkBoxList
Keep incrementing current date by 1 day
As usual I'm just going to paste the code that makes this up. I have comments in their
so any questions let me know.
------------------------------------
events.aspx markup
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Event recurrs which days of the week?"></asp:Label>
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:CheckBoxList>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Enter Time of Event:"></asp:Label>
<br />
<!-- make the text boxes into drop down lists for user -->
<asp:TextBox ID="txtStartTime" runat="server"></asp:TextBox>
<asp:TextBox ID="txtEndTime" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Enter date range for recurring event:"></asp:Label>
<br />
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Enter Event" onclick="Button1_Click" />
</div>
</form>
--------------------------------------
event.aspx.cs
//store event details
protected struct CalEvents
{
public string eventDate;
public string startTime;
public string endTime;
}
protected void Button1_Click(object sender, EventArgs e)
{
DateTime currDate; //used to keep incrementing days
ArrayList aryDays = new ArrayList(); //store DayOfWeek value
ArrayList aryEvents = new ArrayList(); //store array of CalEvents structs
//TODO: properly parse date and time
//make time text boxes into drop down lists for user to select
string startTime = txtStartTime.Text;
string endTime = txtEndTime.Text;
DateTime startDateRange = DateTime.Parse(txtStartDate.Text);
DateTime endDateRange = DateTime.Parse(txtEndDate.Text);
currDate = DateTime.Now.Date;
//store days of week event should be on
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
switch(CheckBoxList1.Items[i].Value)
{
case "Sunday":
aryDays.Add(DayOfWeek.Sunday);
break;
case "Monday":
aryDays.Add(DayOfWeek.Monday);
break;
case "Tuesday":
aryDays.Add(DayOfWeek.Tuesday);
break;
case "Wednesday":
aryDays.Add(DayOfWeek.Wednesday);
break;
case "Thursday":
aryDays.Add(DayOfWeek.Thursday);
break;
case "Friday":
aryDays.Add(DayOfWeek.Friday);
break;
case "Saturday":
aryDays.Add(DayOfWeek.Saturday);
break;
default:
break;
}
}
}
//TODO: add logic to check and make sure startDateRange is
//equal or greater than today's date
//Also check to make sure endDateRange is no more than 12 months out
while ((currDate >= startDateRange) && (currDate <= endDateRange))
{
CalEvents evt = new CalEvents();
for (int i = 0; i < aryDays.Count; i++)
{
if (currDate.DayOfWeek == (DayOfWeek)aryDays[i])
{
evt.eventDate = currDate.Date.ToString();
evt.startTime = startTime;
evt.endTime = endTime;
aryEvents.Add(evt);
}
}
currDate = currDate.AddDays(1.0);
}
for (int i = 0; i < aryEvents.Count; i++)
{
CalEvents evt = new CalEvents();
evt = (CalEvents)aryEvents[i];
//TODO: add logic to write event to database
//debugging purpose only
Response.Write(string.Format("Event Info: {0} <br />", evt.eventDate));
}
}
Of course I am missing code for all of the user input validation, but you can add that as you choose. Happy coding.
47f86058-c8f1-4eae-bc41-460236a6e71b|0|.0
ASP.NET
asp.net