Hi,
I have now solved the problem I had when copying and pasting events. I still have a few issues though that I would appreciate some help on.
I right click on an event and copy (this bit works). When I right click on the the start date for pasting I get both the context menu appear and the window for New event. How do I prevent the New Event window showing on right click. This should only appear on left click.
The paste function still works though but it doesn't refresh the screen. I have to press F5 in order to see the pasted event.
Also the end date of the pasted event is the same as the copied event. This should keep the event the same length after pasting no matter where it is pasted.
Here is my code:
ASPX
<%@ Page Title="Admin Home" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="AdminDefault.aspx.cs" Inherits="TechnicianSchedule.Administration.AdminDefault" %>
<%@ Register Assembly="DayPilot" Namespace="DayPilot.Web.Ui" TagPrefix="DayPilot" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="js/modal.js"></script>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<link href="../Themes/scheduler_white.css" rel="stylesheet" type="text/css" />
<script src="js/event_handling.js" type="text/javascript"></script>
<script type="text/javascript" >
var copied = null;
var modal = new DayPilot.Modal();
modal.border = "10px solid #ccc";
modal.closed = function () {
if (this.result == "OK") {
dps.commandCallBack('refresh');
}
};
function createEvent(start, end, resource) {
modal.height = 250;
modal.showUrl("New.aspx?start=" + start.toStringSortable() + "&end=" + end.toStringSortable() + "&r=" + resource);
}
function editEvent(id) {
modal.height = 300;
modal.showUrl("Edit.aspx?id=" + id);
}
function afterRender(data) {
};
function filter(property, value) {
if (!dps.clientState.filter) {
dps.clientState.filter = {};
}
if (dps.clientState.filter[property] != value) { // only refresh when the value has changed
dps.clientState.filter[property] = value;
dps.commandCallBack('filter');
}
}
</script>
<style type="text/css">
.scheduler_white_rowheader
{
background: -webkit-gradient(linear, left top, left bottom, from(#eeeeee), to(#dddddd));
background: -moz-linear-gradient(top, #eeeeee 0%, #dddddd);
background: -ms-linear-gradient(top, #eeeeee 0%, #dddddd);
background: -webkit-linear-gradient(top, #eeeeee 0%, #dddddd);
background: linear-gradient(top, #eeeeee 0%, #dddddd);
-ms-filter: progid:DXImageTransform.Microsoft.gradient(startColorStr="#eeeeee", endColorStr="#dddddd");
}
.scheduler_white_rowheader_inner
{
border-right: 1px solid #ccc;
}
.scheduler_white_rowheadercol2
{
background: White;
}
.scheduler_white_rowheadercol2 .scheduler_white_rowheader_inner
{
top: 2px;
bottom: 2px;
left: 2px;
background-color: transparent;
border-left: 5px solid #1a9d13; /* green /
border-right: 0px none;
}
.status_dirty.scheduler_white_rowheadercol2 .scheduler_white_rowheader_inner
{
border-left: 5px solid #ea3624; / red /
}
.status_cleanup.scheduler_white_rowheadercol2 .scheduler_white_rowheader_inner
{
border-left: 5px solid #f9ba25; / orange */
}
</style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h1>Administration</h1>
<div style="float: left; width: 150px">
<DayPilot:DayPilotNavigator ID="DayPilotNavigator1" runat="server" BoundDayPilotID="DayPilotScheduler1"
CssClassPrefix="navigator_green_" SelectMode="Week" ShowMonths="3" DataEndField="end"
DataStartField="start" VisibleRangeChangedHandling="CallBack"
OnVisibleRangeChanged="DayPilotNavigator1_VisibleRangeChanged" />
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div style="margin-left: 150px; min-height: 500px">
<DayPilot:DayPilotScheduler ID="DayPilotScheduler1"
runat="server"
DataStartField="EventStart"
DataEndField="EventEnd"
DataTextField="Description"
DataValueField="EventId"
DataResourceField="ResourceId"
DataTagFields="EventType"
ClientObjectName="dps"
TimeFormat="Clock24Hours"
Days="7"
CellDuration="15"
CellGroupBy="Hour"
HeightSpec="Max"
Height="600"
Width="100%"
HeaderFontSize="8pt"
EventFontSize="8pt"
EventMoveHandling="CallBack"
OnEventMove="DayPilotScheduler1_EventMove"
ContextMenuID="DayPilotMenu1"
ContextMenuSelectionID="DayPilotMenuSelection"
EventResizeHandling="CallBack"
OnEventResize="DayPilotScheduler1_EventResize"
EventClickHandling="JavaScript"
EventClickJavaScript="editEvent(e.value());"
TimeRangeSelectedHandling="JavaScript"
TimeRangeSelectedJavaScript="createEvent(start, end, column);"
OnCommand="DayPilotScheduler1_Command"
AfterRenderJavaScript="afterRender(data);"
BusinessBeginsHour="8"
BusinessEndsHour="18"
ShowNonBusiness="false"
OnBeforeEventRender="DayPilotScheduler1_BeforeEventRender" OnBeforeCellRender="DayPilotScheduler1_BeforeCellRender"
CssOnly="true"
CssClassPrefix="scheduler_white"
RowHeaderWidthAutoFit="true"
EventHeight="75"
DurationBarVisible="true"
SyncResourceTree="false"
BubbleID = "DayPilotBubble1"
>
<HeaderColumns>
<DayPilot:RowHeaderColumn Title="Technician" Width="80" />
</HeaderColumns>
</DayPilot:DayPilotScheduler>
<DayPilot:DayPilotMenu ID="DayPilotMenu1" runat="server" CssClassPrefix="menu_default" ShowMenuTitle="true">
<DayPilot:MenuItem Text="Edit..." Action="JavaScript" JavaScript="eventClick(e);"></DayPilot:MenuItem>
<DayPilot:MenuItem Text="Copy" Action="JavaScript" JavaScript="copied = e.value();" ></DayPilot:MenuItem>
</DayPilot:DayPilotMenu>
<DayPilot:DayPilotMenu ID="DayPilotMenuSelection" runat="server" CssClassPrefix="menu_default">
<DayPilot:MenuItem Action="JavaScript" JavaScript="if (!copied) { alert('You need to copy an event first.'); return; } dps.commandCallBack('paste', {id:copied, start: e.start, resource: e.resource});" Text="Paste" />
</DayPilot:DayPilotMenu>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Code Behind
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using DayPilot.Web.Ui;
using DayPilot.Web.Ui.Data;
using DayPilot.Utils;
using DayPilot.Web.Ui.Events.Bubble;
using DayPilot.Web.Ui.Enums;
using DayPilot.Web.Ui.Events.Scheduler;
namespace TechnicianSchedule.Administration
{
public partial class AdminDefault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoadResources();
DayPilotScheduler1.TimeHeaders.Clear();
DayPilotScheduler1.TimeHeaders.Add(new TimeHeader(GroupByEnum.Day));
DayPilotScheduler1.TimeHeaders.Add(new TimeHeader(GroupByEnum.Hour));
DayPilotScheduler1.StartDate = Week.FirstDayOfWeek();
DayPilotScheduler1.Separators.Add(DateTime.Today, Color.Red);
DayPilotScheduler1.DataTagFields = "Location, MarvalRef";
if (!IsPostBack)
{
// Scroll to today
DayPilotScheduler1.SetScrollX(DateTime.Today);
// DataBind();
DayPilotScheduler1.DataSource = dbGetEvents(DayPilotScheduler1.StartDate, DayPilotScheduler1.Days);
DayPilotScheduler1.DataBind();
}
}
protected void DayPilotScheduler1_EventMove(object sender, DayPilot.Web.Ui.Events.EventMoveEventArgs e)
{
string id = e.Value;
DateTime start = e.NewStart;
DateTime end = e.NewEnd;
string resource = e.NewResource;
string message = null;
dbUpdateEvent(id, start, end, resource);
//message = "Reservation moved.";
DayPilotScheduler1.DataSource = dbGetEvents(DayPilotScheduler1.StartDate, DayPilotScheduler1.Days);
DayPilotScheduler1.DataBind();
DayPilotScheduler1.UpdateWithMessage(message);
}
private DataTable dbGetEvents(DateTime start, int days)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT [EventId], [Description], [EventStart], [EventEnd], [ResourceId], [MarvalRef], [Location], [EventType] FROM [Event] WHERE NOT (([EventEnd] <= @start) OR ([EventStart] >= @end))", ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString);
da.SelectCommand.Parameters.AddWithValue("start", start);
da.SelectCommand.Parameters.AddWithValue("end", start.AddDays(days));
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
private void dbUpdateEvent(string id, DateTime start, DateTime end, string resource)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE [Event] SET EventStart = @start, EventEnd = @end, ResourceId = @resource WHERE EventId = @id", con);
cmd.Parameters.AddWithValue("id", id);
cmd.Parameters.AddWithValue("start", start);
cmd.Parameters.AddWithValue("end", end);
cmd.Parameters.AddWithValue("resource", resource);
cmd.ExecuteNonQuery();
}
}
private bool dbIsFree(string id, DateTime start, DateTime end, string resource)
{
// event with the specified id will be ignored
SqlDataAdapter da = new SqlDataAdapter("SELECT count(EventId) as count FROM [Event] WHERE NOT (([EventEnd] <= @start) OR ([EventStart] >= @end)) AND ResourceId = @resource AND EventId <> @id", ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString);
da.SelectCommand.Parameters.AddWithValue("id", id);
da.SelectCommand.Parameters.AddWithValue("start", start);
da.SelectCommand.Parameters.AddWithValue("end", end);
da.SelectCommand.Parameters.AddWithValue("resource", resource);
DataTable dt = new DataTable();
da.Fill(dt);
int count = Convert.ToInt32(dt.Rows[0]["count"]);
return count == 0;
}
private void LoadResources()
{
DayPilotScheduler1.Resources.Clear();
string roomFilter = "0";
if (DayPilotScheduler1.ClientState["filter"] != null)
{
roomFilter = (string)DayPilotScheduler1.ClientState["filter"]["room"];
}
SqlDataAdapter da = new SqlDataAdapter("SELECT [ResourceId], [Name] FROM [Resources] WHERE [Team] <> 50 ORDER BY [OrderAppearance] ", ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString);
da.SelectCommand.Parameters.AddWithValue("beds", roomFilter);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow r in dt.Rows)
{
string name = (string)r["Name"];
string id = Convert.ToString(r["ResourceId"]);
// string status = (string)r["RoomStatus"];
// int beds = Convert.ToInt32(r["RoomSize"]);
//string bedsFormatted = (beds == 1) ? "1 bed" : String.Format("{0} beds", beds);
Resource res = new Resource(name, id);
res.DataItem = r;
// res.Columns.Add(new ResourceColumn(bedsFormatted));
// res.Columns.Add(new ResourceColumn(status));
DayPilotScheduler1.Resources.Add(res);
}
}
protected void DayPilotScheduler1_Command(object sender, DayPilot.Web.Ui.Events.CommandEventArgs e)
{
switch (e.Command)
{
case "refresh":
DayPilotScheduler1.DataSource = dbGetEvents(DayPilotScheduler1.StartDate, DayPilotScheduler1.Days);
DayPilotScheduler1.DataBind();
DayPilotScheduler1.Update();
break;
case "filter":
LoadResources();
DayPilotScheduler1.DataSource = dbGetEvents(DayPilotScheduler1.StartDate, DayPilotScheduler1.Days);
DayPilotScheduler1.DataBind();
DayPilotScheduler1.Update();
break;
case "navigate":
DateTime start = (DateTime)e.Data["start"];
DateTime end = (DateTime)e.Data["end"];
DayPilotScheduler1.StartDate = start;
// DayPilotScheduler1.DataBind();
DayPilotScheduler1.DataSource = dbGetEvents(DayPilotScheduler1.StartDate, DayPilotScheduler1.Days);
DayPilotScheduler1.DataBind();
DayPilotScheduler1.Update(CallBackUpdateType.Full);
break;
case "paste":
DateTime pasteHere = (DateTime)e.Data["start"];
string id = (string)e.Data["id"];
string pasteResource = (string)e.Data["resource"];
// find the event using id
DataRow dr = dbGetEvent(id);
// create a new database record using the original event properties
DateTime pasteEnd = (DateTime)dr["EventEnd"];
string name = (string)dr["Description"];
// int resource = (int)dr["ResourceId"];
string type = (string)dr["EventType"];
string marval = (string)dr["MarvalRef"];
string location = (string)dr["Location"];
dbInsertEvent(pasteHere, pasteEnd, name, pasteResource, type, marval, location);
Modal.Close(this, "OK");
DayPilotScheduler1.DataSource = dbGetEvents(DayPilotScheduler1.StartDate, DayPilotScheduler1.Days);
DayPilotScheduler1.DataBind();
DayPilotScheduler1.UpdateWithMessage("Event copied.");
break;
}
}
private void dbInsertEvent(DateTime pasteHere, DateTime pasteEnd, string name, string pasteResource, string type, string marval, string location)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [Event] (EventStart, EventEnd, Description, ResourceId, EventType, MarvalRef, Location) VALUES(@start, @end, @name, @resource, @type, @marval, @location)", con);
//cmd.Parameters.AddWithValue("id", id);
cmd.Parameters.AddWithValue("start", pasteHere);
cmd.Parameters.AddWithValue("end", pasteEnd);
cmd.Parameters.AddWithValue("name", name);
cmd.Parameters.AddWithValue("resource", pasteResource);
cmd.Parameters.AddWithValue("type", type);
cmd.Parameters.AddWithValue("marval", marval);
cmd.Parameters.AddWithValue("location", location);
cmd.ExecuteNonQuery();
}
}
private DataRow dbGetEvent(string id)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Event] WHERE EventId = @id", ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString);
da.SelectCommand.Parameters.AddWithValue("id", id);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
return dt.Rows[0];
}
return null;
}
protected void DayPilotScheduler1_EventResize(object sender, DayPilot.Web.Ui.Events.EventResizeEventArgs e)
{
string id = e.Value;
DateTime start = e.NewStart;
DateTime end = e.NewEnd;
string resource = e.Resource;
string message = null;
dbUpdateEvent(id, start, end, resource);
//message = "Reservation updated.";
DayPilotScheduler1.DataSource = dbGetEvents(DayPilotScheduler1.StartDate, DayPilotScheduler1.Days);
DayPilotScheduler1.DataBind();
DayPilotScheduler1.UpdateWithMessage(message);
}
protected void DayPilotScheduler1_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.Scheduler.BeforeEventRenderEventArgs e)
{
e.Html = String.Format("{0}", e.Text);
string type = Convert.ToString(e.DataItem["EventType"]);
switch (type)
{
case "0": // install
e.DurationBarColor = "green";
e.ToolTip = "Installation";
break;
case "1": // consultancy
e.DurationBarColor = "blue";
e.ToolTip = "Consultancy";
break;
case "2": // incident
e.DurationBarColor = "orange";
e.ToolTip = "Incident";
break;
case "3": // abscence
e.DurationBarColor = "gray";
e.ToolTip = "Absence";
break;
case "4": // meeting
e.DurationBarColor = "yellow";
e.ToolTip = "Meeting";
break;
case "5": // Duty
e.DurationBarColor = "purple";
e.ToolTip = "Duty";
break;
case "6": // Curriculum Support
e.DurationBarColor = "#FF99CC";
e.ToolTip = "Curriculum Support";
break;
case "7": // Project
e.DurationBarColor = "#FF00FF";
e.ToolTip = "Project Work";
break;
case "8": // Admin
e.DurationBarColor = "#000080";
e.ToolTip = "Admin";
break;
case "9": // other
e.DurationBarColor = "#CCFFFF";
e.ToolTip = "Other";
break;
case "10": //Training
e.DurationBarColor = "#FF0000";
e.ToolTip = "Training";
break;
case "11": // On Site Training
e.DurationBarColor = "#99CC00";
e.ToolTip = "On Site Training";
break;
default:
throw new ArgumentException("Unexpected type.");
}
e.Html = e.Html + String.Format("<br /><span style='color:gray'>{0}</span>", e.ToolTip);
string location = Convert.ToString(e.DataItem["Location"]);
string locationColor = "#222222";
string marval = Convert.ToString(e.DataItem["MarvalRef"]);
string marvalColor = "#FF0000";
e.Areas.Add(new Area().Bottom(25).Left(4).Html("<div style='color:" + marvalColor + "; font-size: 9pt;'>Marval Ref: " + marval + "</div>").Visibility(AreaVisibility.Visible));
e.Areas.Add(new Area().Bottom(10).Right(4).Html("<div style='color:" + locationColor + "; font-size: 9pt;'>Location: " + location + "</div>").Visibility(AreaVisibility.Visible));
// e.Areas.Add(new Area().Left(4).Bottom(8).Right(4).Height(2).Html("<div style='background-color:" + locationColor + "; height: 100%; width:" + location + "%'></div>").Visibility(AreaVisibility.Visible));
}
protected void DayPilotScheduler1_BeforeCellRender(object sender, DayPilot.Web.Ui.Events.BeforeCellRenderEventArgs e)
{
if (e.IsBusiness)
{
e.BackgroundColor = "#ffffff";
}
else
{
e.BackgroundColor = "#f8f8f8";
}
if (e.Start.Date == DateTime.Today)
{
e.BackgroundColor = "#ffffcc";
}
if (e.Start.TimeOfDay.Hours == 17)
{
e.BackgroundColor = "#f1f1f1";
}
if (e.ResourceId == "11")
{
e.BackgroundColor = "orange";
}
else if (e.ResourceId == "12")
{
e.BackgroundColor = "green";
}
else if (e.ResourceId == "19")
{
e.BackgroundColor = "blue";
}
else if (e.ResourceId == "20")
{
e.BackgroundColor = "pink";
}
}
protected void DayPilotNavigator1_VisibleRangeChanged(object sender, DayPilot.Web.Ui.Events.Navigator.VisibleRangeChangedEventArgs e)
{
DayPilotNavigator1.DataBind();
}
protected void DayPilotBubble1_RenderEventBubble(object sender, RenderEventBubbleEventArgs e)
{
e.InnerHTML = "<b>Event Details</b><br />" + e.Text + "<br />" + e.Start + " To " + e.End + "<br /> At " + e.Tag["Location"] + "<br />Marval Ref: " + e.Tag["MarvalRef"];
}
}
}