|
|
Try the online demo:
AJAX-style event creating, moving, resizing, and deleting
Context menu
Day view
Work week view
Week view
Month view
Horizontal/vertical resources view
PostBack/AJAX/JavaScript event handling
Binding to XmlDataSource, SqlDataSource, DataTable, ArrayList
Custom event formatting
UpdatePanel compatibility
All chapters on a single page1. InstallationYou don't need to install anything to start using the DayPilot component . Just reference the assembly in your project and go on. However, it's also possible to add the DayPilot to yout Visual Studio 2005 Toolbox so you drag the component onto your web form and set the properties visually. Adding the component to the Visual Studio 2005 Toolbox: 1. Right-click the toolbox and select "Choose Items...". 2. In the "Choose Toolbox Items" dialog click "Browse...". 2. Find the DayPilot assembly and click OK. 3. Close the "Choose Toolbox Items" dialog by clicking "OK" and you will see the DayPilot icon in the toolbox: 4. Now you can drag the icon to your WebForm and you will see the DayPilot object: 5. You can set the DayPilot properties in the "Properties" window: 2. Data bindingThe only required step to make DayPilot working is to bind it to a data source. You can either set DataSource property directly or use DataSourceID. Let's have a following table:
In order to show the events in DayPilot calendar you have to do the following steps:
Setting the DataSource propertyAfter loading a DataTable from a database (or other source) you should assign it to the DayPilotCalendar.DataSource property: DayPilotCalendar1.DataSource = MyDataTable; In our example we are building the DataTable by hand: DataTable dt;
dt= new DataTable();
dt.Columns.Add("start", typeof(DateTime));
dt.Columns.Add("end", typeof(DateTime));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("id", typeof(string));
DataRow dr;
dr = dt.NewRow();
dr["id"] = 0;
dr["start"] = Convert.ToDateTime("15:30").AddDays(1);
dr["end"] = Convert.ToDateTime("16:30").AddDays(1);
dr["name"] = "Partner conf. call";
dt.Rows.Add(dr);
// ...
return dt;
When loading the events from a database I recommend limiting the SELECT so only the necessary events are loaded (not all events from the table). DayPilot will work properly in both cases (it only select the relevant events) but all the events will have to be loaded and they will be stored in the ViewState. Setting column name propertiesYou need to indicate which columns contain the necessary data: DayPilotCalendar1.DataStartField = "start"; DayPilotCalendar1.DataEndField = "end"; DayPilotCalendar1.DataTextField = "name"; DayPilotCalendar1.DataValueField = "id"; You can also set these properties in the visual designer: Setting the visible datesLet's say we want to show just a single day: DayPilotCalendar1.StartDate = Convert.ToDateTime("1 June 2006");
But we can show multiple days as well. This is a new feature of DayPilot 2.0. DayPilotCalendar1.StartDate = Convert.ToDateTime("1 June 2006");
DayPilotCalendar1.Days = 5;
Example: Data bindingBind the data in the Page_Load() method: if (!IsPostBack) DataBind(); 3. Data-related properties overviewHere are the data-related properties of DayPilotCalendar:
4. Integration with System.Web.Ui.WebControls.CalendarFor switching the date you can use the standard .NET Framework control System.Web.UI.WebControls.Calendar. You can use the PostBack event to change the DayPilot StartDate and Days properties. In our sample we will use the DayRender event to improve the calendar:
private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
string fontWeight = "normal";
if (isThereEvent(e.Day.Date))
fontWeight = "bold";
string color = "black";
if (e.Day.IsOtherMonth)
color = this.Calendar1.OtherMonthDayStyle.ForeColor.Name;
e.Cell.Text = String.Format("<a href='Default.aspx?day={0:d}' style='color: "
+ color + ";text-decoration:none; font-weight:"
+ fontWeight + "'>{1}</a>", e.Day.Date, e.Day.Date.Day);
}
The method isThereEvent() returns true if a specific day contains any event. This method will be specific to your application. You can go through the data returned from the database (and supplied to DayPilotCalendar.DataSource) to avoid another database request. We are not using the database in our sample so it is hard-coded: private bool isThereEvent(DateTime date)
{
DateTime today = DateTime.Now;
DateTime tomorrow = today.AddDays(1);
DateTime anotherDay = today.AddDays(3);
// there are events today
if ((date.DayOfYear == today.DayOfYear) && (date.Year == today.Year))
return true;
// there are events tomorrow
if ((date.DayOfYear == tomorrow.DayOfYear) && (date.Year == tomorrow.Year))
return true;
// there are events on another day
if ((date.DayOfYear == anotherDay.DayOfYear) && (date.Year == anotherDay.Year))
return true;
return false;
}5. Changing the appearanceThere are many options to change the default appearance:
For appearance examples see the screenshots page. 6. Handling user actionsThere are two types of user actions:
The actions can be handled on the client (by custom JavaScript code) or on the server (by handling the server event).
For more about server-side event handling see Server-side event handling. UsageThe typical action for clicking a free time slot will be creating a new event. You can use the following JavaScript code to redirect the user to a page with new event details: Set JavaScriptFreeAction to "document.location='NewEvent.aspx?startingtime={0}';" In the NewEvent.aspx take the Request.QueryString["startingtime"] value and convert it to DateTime: DateTime startingTime; if (Request.QueryString["startingtime"] != null) startingTime = Convert.ToDateTime(Request.QueryString["startingtime"]); Then you can use the value to prefill a form with date and time specification. 7. Server-side event handling
Your server-side event handling methods will get the important data: EventClick knows the event value (PK column)The argument of EventClick handler is set to the value of the clicked event (DataValueField). private void DayPilotCalendar1_EventClick(string pk)
{
Label1.Text = "Selected event: " + pk;
}
FreeTimeClick knows the time clickedprivate void DayPilotCalendar1_FreeTimeClick(System.DateTime start)
{
Label1.Text = "Selected time: " + start.ToString("s");
}8. Choosing DayFormat - DateTime.ToString() parametersThe DayFormat property specifies the DateTime format to be used for day headers. It accepts the same string as DateTime.ToString(string). Standard format examplesHere are the examples for EN-US locale:
Custom format examples
DayPilot Pro is an advanced DayPilot edition. You can check a thumbnail overview of the most interesting features. There is also an online demo with all the features working (including the AJAX features). If you want to test the design-time support and API you can download a fully functional trial version. And if you like it, you can buy a full version with source code and 12 months of upgrades and support (with a 30-days money back guarantee). DayPilot Lite is a do-it-yourself open-source edition of DayPilot. Although it misses some DayPilot Pro features, there are thousands of developers using it to build calendar, personal scheduling, and resource booking applications. |