search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Null Exception on DataItem when setting StartDate

Asked by Scott
12 days ago.

Ok, issue with this code. But using a Scheduler instead of a Calendar...
https://kb.daypilot.org/37327/how-to-show-calendar-events-with-custom-background-color-in/

protected void DayPilotScheduler1_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.Scheduler.BeforeEventRenderEventArgs e)
{
string color = e.DataItem["color"] as string; // null exception here
if (!String.IsNullOrEmpty(color))
{
e.DurationBarColor = color;
}
}

The above works fine on initial rendering. However, if you have a 'previous week' and 'next week' button which sets the StartDate the above event triggers and the DataItem field is null.

// example prev week button
protected void btnPrevWeek_Click(object sender, EventArgs e)
{
DayPilotScheduler1.StartDate = DayPilotScheduler1.StartDate.AddDays(-7);
}

Answer posted by Dan Letecky [DayPilot]
12 days ago.

Hi Scott,

The e.DataItem property is only initialized if you call DataBind(). Calling DataBind() is not necessary during callbacks/postbacks because the events are reloaded from the ViewState. However, there is no reference to the original objects in this case.

If you want to make the "color" field accessible during callbacks and postbacks without calling DataBind(), you need to load it to "tags":

<DayPilot:DayPilotScheduler
  DataTagFields="color"
...

Then you'll be able to access the color field in BeforeEventRender:

protected void DayPilotScheduler1_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.Scheduler.BeforeEventRenderEventArgs e) 
{ 
  string color = e.Tag["color"];
  if (!String.IsNullOrEmpty(color)) 
  { 
    e.DurationBarColor = color; 
  } 
}

Some more details can be found here:
https://doc.daypilot.org/scheduler/event-loading/

New Reply
This reply is
Your name (optional):