Hi again,
I'm not sure if this is the correct way of doing it as it seems like quite a bit of coding to get at the answer! I've probably missed something very obvious along the way but at least this code works. I will post it here in case anyone else may need it.
Remember for my application I only ever let the user have one event per day and the event runs all day. So the following code will allow you to find one event per day. For example if you were looking to find the event for '09-Sep-2011' this will return the first event it finds on that day.
public int GetEventIdByDate(DateTime StartDate)
{
// Default Result
int result = 0;
// Create the End Dates (I'm only ever looking for one particular day)
DateTime EndDate = StartDate.AddDays(1);
DateTime EndDatePlusOneDay = EndDate.AddDays(1);
// Grab the events (including recurring) for the date specified in StartDate
string Sql = "SELECT e.[id], [name], e.[eventstart], e.[eventend], recurrence FROM [Events] e WHERE NOT ((e.[eventend] <= '" + StartDate.ToLongDateString() + "') OR (e.[eventstart] >= '" + EndDatePlusOneDay.ToLongDateString() + "')) or (e.[recurrence] is not null and e.[eventstart] < '" + StartDate.ToLongDateString() + "')";
// The RunSQL bit is just a utility function to execute SQL and return a DataSet
DataTable dt = this.RunSQL(Sql).Tables[0];
// Create a DayPilotMonth Control. I guess it should really be a calendar control where you can restrict the days to just 1, but it works for my purposes
DayPilot.Web.Ui.DayPilotMonth dm = new DayPilot.Web.Ui.DayPilotMonth();
dm.DataEndField = "eventend";
dm.DataStartField = "eventstart";
dm.DataTextField = "name";
dm.DataValueField = "id";
dm.DataTagFields = "id, name";
dm.DataSource = this.RunSQL(Sql).Tables[0];
dm.StartDate = new DateTime(StartDate.Year, StartDate.Month, 1);
// Set the Recurrent Properties
dm.DataRecurrenceField = "recurrence";
dm.RecurrentEventImage = "~/images/recur10x9.png";
dm.RecurrentEventExceptionImage = "~/images/recurex10x9.png";
// Set a BeforeEventRender so we can collect the events into a list
dm.BeforeEventRender += this.DayPilot_BeforeEventRender;
dm.DataBind();
// We're going to render the Control so we can get the events (including recurring)
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
// We don't want to render the control on our actual page, so create a placeholder one
Page pg = new Page();
pg.Controls.Add(dm);
gEvents.Clear();
dm.RenderControl(hw);
// Now we've created our list in function DayPilot_BeforeEventRender, we can get rid of the page and control
pg.Controls.Clear();
dm.Dispose();
pg.Dispose();
// Let's see if we've found an event for the day in question. For my purposes there's only ever one event per day.
if (gEvents.Count > 0)
{
for (int i=0; i < gEvents.Count; i++)
{
// Does this event match the dat we're looking for?
if (gEvents[i].Start == StartDate)
{
// Grab the Id so we can return it.
result = Convert.ToInt32(gEvents[i].Tag["id"]);
break;
}
}
}
// Returns 0 or the Event Id if it found it.
return result;
}
protected void DayPilot_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.Month.BeforeEventRenderEventArgs e)
{
// Add each event to a globally defined list collection... the declaration is below..
// List<DayPilot.Web.Ui.Events.Month.BeforeEventRenderEventArgs> gEvents = new List<DayPilot.Web.Ui.Events.Month.BeforeEventRenderEventArgs>();
gEvents.Add(e);
}