Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

   2023-02-08 学习力0
核心提示:DownloadsTutorialShiftScheduling.VS2010.20130629.zip 1228 kBVisual Studio 2010 solution with the tutorial source code. Microsoft SQL Server 2008 database. Includes C# and VB projects.Sample ProjectThe sample project includes:DayPilot Pro 7.

Downloads

Sample Project

The sample project includes:

Online Demo

Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

This tutorial shows how to display a shift schedule for multiple  locations/positions.

Features:

  • Overview page displaying all locations and assignments
  • Easy assignment creating and moving using drag&drop
  • Recurring assignments
  • Asynchronouse detection of conflicting assignments

1. Shift Schedule Overview Page

Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

The shift schedule overview page uses DayPilot Scheduler to show assignments  for all locations.

Locations are displayed on the Y axis.

C#

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    LoadResources();
    DayPilotScheduler1.StartDate = Week.FirstDayOfWeek(DateTime.Now);
    DayPilotScheduler1.DataSource = new DataManager().GetAssignments(DayPilotScheduler1);
    DayPilotScheduler1.DataBind();

    DayPilotScheduler1.SetScrollX(DateTime.Today);
  }
}

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  If Not IsPostBack Then
    LoadResources()
    DayPilotScheduler1.StartDate = Week.FirstDayOfWeek(Date.Now)
    DayPilotScheduler1.DataSource = (New DataManager()).GetAssignments(DayPilotScheduler1)
    DayPilotScheduler1.DataBind()

    DayPilotScheduler1.SetScrollX(Date.Today)
  End If
End Sub

The Resources collection is filled using data from  [Location] table:

C#

private void LoadResources()
{
  DataTable locations = new DataManager().GetLocations();
  DayPilotScheduler1.Resources.Clear();
  foreach(DataRow dr in locations.Rows)
  {
    DayPilotScheduler1.Resources.Add((string)dr["LocationName"], Convert.ToString(dr["LocationId"]));
  }
} 

VB.NET

Private Sub LoadResources()
  Dim locations As DataTable = (New DataManager()).GetLocations()
  DayPilotScheduler1.Resources.Clear()
  For Each dr As DataRow In locations.Rows
    DayPilotScheduler1.Resources.Add(CStr(dr("LocationName")), Convert.ToString(dr("LocationId")))
  Next dr
End Sub

2. Assignments by Location

Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

This page shows assignment for a location selected using a  DropDownList.

It uses delayed loading - the selected location is detected  on the client side using jQuery and a calendar refresh is requested:

<script type="text/javascript">
  function updateCalendar() {
    dp.clientState.location = parseInt($("#MainContent_DropDownListLocation").val());
    dp.commandCallBack('refresh');
  }

  $(document).ready(function () {
    updateCalendar();
  });
</script>

The selected location is stored in the ClientState property (ClientState["location"]) and is persisted during the subsequent callbacks.

The ClientState is updated and a refresh is requested whenever the users  selects a new location using the DropDownList (see onchange attribute).

 <div>Location: 
  <asp:DropDownList 
    ID="DropDownListLocation" 
    runat="server"
    DataTextField="LocationName"
    DataValueField="LocationId"
    onchange="updateCalendar();"
  />
</div> 

The JavaScript code uses commandCallBack() method to fire Command event on the server side. 

C#

protected void DayPilotCalendar1_Command(object sender, CommandEventArgs e)
{
  switch (e.Command)
  {
    // ...
    case "refresh":
      DayPilotCalendar1.DataSource = new DataManager().GetAssignmentsForLocation(DayPilotCalendar1);
      DayPilotCalendar1.DataBind();
      DayPilotCalendar1.Update();
      break;
    // ...
  }
}

VB.NET

Protected Sub DayPilotCalendar1_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
  Select Case e.Command
    '...
    Case "refresh"
      DayPilotCalendar1.DataSource = (New DataManager()).GetAssignmentsForLocation(DayPilotCalendar1)
      DayPilotCalendar1.DataBind()
      DayPilotCalendar1.Update()
    End Select
End Sub

The selected location is loaded from the ClientState property in GetAssignmentsForLocation() method:

C#

public DataTable GetAssignmentsForLocation(DayPilotCalendar calendar)
{
  DataTable dt = new DataTable();

  // ...

  SqlDataAdapter da = new SqlDataAdapter("select * from [Assignment] join [Person] on [Assignment].[PersonId] = [Person].[PersonId] where NOT (([AssignmentEnd] <= @start) OR ([AssignmentStart] >= @end)) and [LocationId] = @location and [AssignmentRecurrence] is null", ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString);
  da.SelectCommand.Parameters.AddWithValue("start", calendar.StartDate);
  da.SelectCommand.Parameters.AddWithValue("end", calendar.EndDate.AddDays(1));
  da.SelectCommand.Parameters.AddWithValue("location", (int) calendar.ClientState["location"]);
  da.Fill(dt);
  return dt;
}

VB.NET

Public Function GetAssignmentsForLocation(ByVal calendar As DayPilotCalendar) As DataTable
  Dim dt As New DataTable()

  ' ...

  Dim da As New SqlDataAdapter("select * from [Assignment] join [Person] on [Assignment].[PersonId] = [Person].[PersonId] where NOT (([AssignmentEnd] <= @start) OR ([AssignmentStart] >= @end)) and [LocationId] = @location and [AssignmentRecurrence] is null", ConfigurationManager.ConnectionStrings("daypilot").ConnectionString)
  da.SelectCommand.Parameters.AddWithValue("start", calendar.StartDate)
  da.SelectCommand.Parameters.AddWithValue("end", calendar.EndDate.AddDays(1))
  da.SelectCommand.Parameters.AddWithValue("location", CInt(calendar.ClientState("location")))
  da.Fill(dt)
  Return dt
End Function

3. Assignments by Person

Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

This page shows assignments for the selected person (DropDownList) in a week view using DayPilot Calendar control.

Again, we are storing the selected person id in the ClientState and calling  commandCallBack('refresh') after the first page load.

<script type="text/javascript">
  function updateCalendar() {
    dp.clientState.person = parseInt($("#MainContent_DropDownListPerson").val());
    dp.commandCallBack('refresh');
  }

  $(document).ready(function () {
    updateCalendar();
  });
</script>

We send a request for update after every DropDownListPerson  change:

 <div>Person: 
  <asp:DropDownList 
    ID="DropDownListPerson" 
    runat="server"
    DataTextField="PersonFullName"
    DataValueField="PersonId"
    onchange="updateCalendar();"
/>
</div> 

The Command event handler reloads the events.

C#

protected void DayPilotCalendar1_Command(object sender, CommandEventArgs e)
{
  switch (e.Command)
  {
    // ...
    case "refresh":
      DayPilotCalendar1.DataSource = new DataManager().GetAssignmentsForPerson(DayPilotCalendar1);
      DayPilotCalendar1.DataBind();
      DayPilotCalendar1.Update();
      break;
  }
}

VB.NET

Protected Sub DayPilotCalendar1_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
  Select Case e.Command
    ' ...
    Case "refresh"
      DayPilotCalendar1.DataSource = (New DataManager()).GetAssignmentsForPerson(DayPilotCalendar1)
      DayPilotCalendar1.DataBind()
      DayPilotCalendar1.Update()

  End Select
End Sub

The selected person id is loaded from the ClientState:

C#

public DataTable GetAssignmentsForPerson(DayPilotCalendar calendar)
{
  DataTable dt = new DataTable();

  // ...

  SqlDataAdapter da = new SqlDataAdapter("select * from [Assignment] join [Location] on [Assignment].[LocationId] = [Location].[LocationId] where NOT (([AssignmentEnd] <= @start) OR ([AssignmentStart] >= @end)) and [PersonId] = @person and [AssignmentRecurrence] is null", ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString);
  da.SelectCommand.Parameters.AddWithValue("start", calendar.StartDate);
  da.SelectCommand.Parameters.AddWithValue("end", calendar.EndDate.AddDays(1));
  da.SelectCommand.Parameters.AddWithValue("person", (int)calendar.ClientState["person"]);
  da.Fill(dt);
  return dt;
}

VB.NET

Public Function GetAssignmentsForPerson(ByVal calendar As DayPilotCalendar) As DataTable
  Dim dt As New DataTable()

  ' ...

  Dim da As New SqlDataAdapter("select * from [Assignment] join [Location] on [Assignment].[LocationId] = [Location].[LocationId] where NOT (([AssignmentEnd] <= @start) OR ([AssignmentStart] >= @end)) and [PersonId] = @person and [AssignmentRecurrence] is null", ConfigurationManager.ConnectionStrings("daypilot").ConnectionString)
  da.SelectCommand.Parameters.AddWithValue("start", calendar.StartDate)
  da.SelectCommand.Parameters.AddWithValue("end", calendar.EndDate.AddDays(1))
  da.SelectCommand.Parameters.AddWithValue("person", CInt(calendar.ClientState("person")))
  da.Fill(dt)
  Return dt
End Function

4. Agenda

Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

Agenda is a simple GridView of the upcoming assignments for  the selected person.

<div>Person: 
  <asp:DropDownList 
    ID="DropDownListPerson" 
    runat="server"
    DataTextField="PersonFullName"
    DataValueField="PersonId" 
    onselectedindexchanged="DropDownListPerson_SelectedIndexChanged"
    AutoPostBack="true"
    AppendDataBoundItems="true">
  </asp:DropDownList>
</div> 

<asp:GridView ID="GridViewAgenda" runat="server" AutoGenerateColumns="False">
  <Columns>
    <asp:BoundField DataField="AssignmentStart" HeaderText="Start" />
    <asp:BoundField DataField="AssignmentEnd" HeaderText="End" />
    <asp:BoundField DataField="LocationName" HeaderText="Location" />
    <asp:BoundField DataField="AssignmentNote" HeaderText="Note" />
  </Columns>
</asp:GridView>

It is updated using traditional PostBack for simplicity:

C#

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    DropDownListPerson.DataSource = new DataManager().GetPeople();

    if (IsSelected)
    {
      DropDownListPerson.SelectedValue = Request.QueryString["id"];
    }
    else
    {
      DropDownListPerson.SelectedIndex = 0;
    }
    DropDownListPerson.DataBind();

    DataTable dt = new DataManager().GetAssignmentsForPersonExpanded(PersonId, DateTime.Now, DateTime.Now.AddMonths(1));
    GridViewAgenda.DataSource = dt;

    Label1.Text = dt.Rows.Count.ToString();

    GridViewAgenda.DataBind();
  }
}

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  If Not IsPostBack Then
    DropDownListPerson.DataSource = (New DataManager()).GetPeople()

    If IsSelected Then
      DropDownListPerson.SelectedValue = Request.QueryString("id")
    Else
      DropDownListPerson.SelectedIndex = 0
    End If
    DropDownListPerson.DataBind()

    Dim dt As DataTable = (New DataManager()).GetAssignmentsForPersonExpanded(PersonId, Date.Now, Date.Now.AddMonths(1))
    GridViewAgenda.DataSource = dt

    Label1.Text = dt.Rows.Count.ToString()

    GridViewAgenda.DataBind()
  End If
End Sub

5. Conflict Warning

Conflict means there are two or more concurrent assignments for the same  person.

Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

The conflicts are not being detected before an assignment change is stored in  the database but asynchronously after every page call. Pages  with DayPilot controls run the afterRender() function using AfterRenderJavaScript:

<DayPilot:DayPilotScheduler
  ID="DayPilotScheduler1" 
  runat="server" 
  ...
  AfterRenderJavaScript="afterRender();"
/>

All other pages run it using jQuery:

 <script type="text/javascript">
  $(document).ready(function () {
    afterRender();
  });
</script>

The afterRender() function sends an AJAX request to  ConflictCount.aspx and updates the menu with the returned HTML.

function afterRender() {
  $.get('ConflictCount.aspx', function (data) {
    $('#conflicts').html(data);
  });
}

ConflictCount.aspx uses the ConflictDetector class to check  for blocks of overlapping events and return the number of conflicts.

C#

protected void Page_Load(object sender, EventArgs e)
{
  Response.Cache.SetCacheability(HttpCacheability.NoCache);

  DateTime start = DateTime.Today;
  DateTime end = DateTime.Today.AddMonths(1);

  DataTable data = new DataManager().GetAssignmentsExpanded(start, end);
  var detector = new ConflictDetector();
  detector.Load(data, "AssignmentStart", "AssignmentEnd", "PersonId", start, end);

  Response.Clear();
  if (detector.Count > 0)
  {
    Response.Write(String.Format("<span style='background-color:red; color:white;'>({0})</span>", detector.Count));
  }
  else
  {
    Response.Write(String.Format("<span>({0})</span>", detector.Count));
  }
} 

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  Response.Cache.SetCacheability(HttpCacheability.NoCache)

  Dim start As Date = Date.Today
  Dim [end] As Date = Date.Today.AddMonths(1)

  Dim data As DataTable = (New DataManager()).GetAssignmentsExpanded(start, [end])
  Dim detector = New ConflictDetector()
  detector.Load(data, "AssignmentStart", "AssignmentEnd", "PersonId", start, [end])

  Response.Clear()
  If detector.Count > 0 Then
    Response.Write(String.Format("<span style='background-color:red; color:white;'>({0})</span>", detector.Count))
  Else
    Response.Write(String.Format("<span>({0})</span>", detector.Count))
  End If
End Sub

6. List of Conflicts

Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

The Conflicts page uses ConflictDetector class to analyze a data set  for overlapping events.

C#

protected void Page_Load(object sender, EventArgs e)
{

  if (!IsPostBack)
  {
    DateTime start = DateTime.Today;
    DateTime end = DateTime.Today.AddMonths(1);

    DataTable data = new DataManager().GetAssignmentsExpanded(start, end);

    var detector = new ConflictDetector();
    detector.Load(data, "AssignmentStart", "AssignmentEnd", "PersonId", start, end);
    GridView1.DataSource = detector.List;

    Label1.Text = detector.List.Count.ToString();
    DataBind();
  }
}

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

  If Not IsPostBack Then
    Dim start As Date = Date.Today
    Dim [end] As Date = Date.Today.AddMonths(1)

    Dim data As DataTable = (New DataManager()).GetAssignmentsExpanded(start, [end])

    Dim detector = New ConflictDetector()
    detector.Load(data, "AssignmentStart", "AssignmentEnd", "PersonId", start, [end])
    GridView1.DataSource = detector.List

    Label1.Text = detector.List.Count.ToString()
    DataBind()
  End If
End Sub

The ConflictDetector accepts a DataTable with event records.  You need to specify the column with assignment start ("AssignmentStart"),  assignment end ("AssignmentEnd"), and with resource identifier ("PersonId"). It  will check for overlapping events with the same resource id, within the range  specified by "start" and "end" variables.

ConflictDetector.List holds a list of Conflict classes which  contain details about the conflicts:

public class Conflict
{
  public List<ConflictEvent> Events;  // List of conflicting events
  public DateTime Start; // Start of the conflict (start of the first conflicting event in this block).
  public DateTime End; // End of the conflict (end of the last conflicting event in this block).
  public int Level; // Conflict level. The max parallel event count.
  public string Resource; // Resource id.
}

ConflictEvent class has information about the source event:

 public class ConflictEvent
{
  public DateTime Start; // Event start
  public DateTime End;  // Event end
  public string Value; // Event id
  public int Position; // Position of the event in a block of overlapping events, corresponds to the column number when displayed in the Calendar control
  public DataItemWrapper DataItem; // Source data object (DataRow)
}

The detected conflicts are displayed using a simple GridView:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
  <Columns>
    <asp:TemplateField HeaderText="Conflict">
      <ItemTemplate>
        <a href='People.aspx?person=<%# Eval("Resource") %>&date=<%# Convert.ToDateTime(Eval("Start")).ToString("s") %>'>Show</a>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Person">
      <ItemTemplate>
        <%# Eval("Events[0].DataItem.Source[\"PersonFullName\"]")%>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Start" HeaderText="Start" />
    <asp:BoundField DataField="End" HeaderText="End" />
    <asp:TemplateField HeaderText="Events in Conflict">
      <ItemTemplate>
        <%# Eval("Events.Count") %>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

8. Recurring Events

Adding recurrence support complicated the situation on all levels. Allowing  recurring events requires special handling at these places:

  1. Adding [AssignmentRecurrence] field to the [Assignment] database table.
  2. Extending the New.aspx and Edit.aspx dialogs with the recurrence rule definition support (UI and database storage).
  3. Extending all database loading methods to load recurrence definitions. Regular events can be loaded using the StartDate and EndDate range but the recurrence definitions must be loaded from the past as well.
  4. Detecting the recurring events in the update events (EventResize, EventMove).
  5. Expanding the rules into individual occurrences manually before showing the data in a GridView (Agenda.aspx).

For simplicity, the exceptions from the recurrence rule and not allowed.

See also:

7. Database Schema (SQL Server)

Shift Scheduling Tutorial (ASP.NET, SQL Server, C#, VB.NET)

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Windows API Reference for C#, VB.NET
    不错的.net 下用API的参考站点地址在:http://www.webtropy.com/articles/Win32-API-DllImport-art9.asp 下面摘抄分类,便于大家直接就拿来用: File, Memory, Process, Threading, Time, Console, and Comm control(kernel32.dll) _hread_hwrite_lclose_lcr
    03-16
  • 一个基于API的VB.net串口通讯类 vbnet串口通信
    VB.net的串口通讯支持总是让人觉得有所不足,在使用VB6的MsComm32.ocx时,很多人都会在VB.net的开发中觉得很困扰。    这里讲述的VB.net串口通讯类使用native代码,并且它是通API调用实现的,你会发现VB.net的串口通讯就是这么简单。    在说明如何使
    02-12
  • [VB][ASP.NET]FileUpload控件「批次上传 / 多档
    FileUpload控件「批次上传 / 多档案同时上传」的范例 (VB语法) http://www.dotblogs.com.tw/mis2000lab/archive/2008/05/14/3986.aspx    FileUpload控件真的简单好用,不使用它来作批次上传,却要改用别的方法,实在不聪明。要用就一次用到底,公开File
    02-10
  • 第八章 VB中ActiveX控件的使用
    轉自:http://wwww.hyit.edu.cn/edu/vb/study/index.htm第八章          VB中ActiveX控件的使用8.1  概述     这里的ActiveX控件是指VB标准工具箱里没有的控件,用时需从“工程”菜单里选择“部件…”(或右键单击工具箱,从快捷菜单中选择“部
    02-10
  • 第二章 VB的界面设计
    轉自:http://wwww.hyit.edu.cn/edu/vb/study/index.htm第二章         VB的界面设计2.1  VB用户界面设计基础1. 概述   界面的设计有两步:先绘制控件,然后确定控件属性。   绘制控件:在工具箱里单击想画的控件,在窗体里按下鼠标并拖曳,然后
    02-10
  • C#/VB.NET 获取Excel中图片所在的行、列坐标位置
    C#/VB.NET 获取Excel中图片所在的行、列坐标位
    本文以C#和vb.net代码示例展示如何来获取Excel工作表中图片的坐标位置。这里的坐标位置是指图片左上角顶点所在的单元格行和列位置,横坐标即顶点所在的第几列、纵坐标即顶点所在的第几行。下面是获取图片位置的详细方法及步骤。【程序环境】按照如下方法来引
    02-09
  • VB操作XML
    VB操作XML
    XSL(可扩展样式表语言)是对CSS的一种扩展,功能比CSS强大得多。XML链接是在HTML链接的功能之上加以扩展,可以支持更为复杂的链接,通过XML链接,不仅可以在XML文件之间建立链接,还可以建立其他类型数据之间的链接,其规范分为三个部分:XLink语言,XPointe
    02-09
  • VB6多线程,关键段操作 vb6.0 多线程
    Option Explicit Declare Function GetLastError Lib "kernel32" () As Long 'Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 'Declare Sub ExitThread Lib "kernel32" (Optional ByVal dwExitCode
    02-09
  • VB.NET调用IE,并且等待
                Dim p As New Process            '获得URL            aURL = GetURL()            '获得IE路径            p.StartInfo.FileName = System.Environment.GetFolderPath( _ 
    02-09
  • vb的VSFlexGrid控件 vb msflexgrid
    多行选中VSFlexGrid的SelectionMode = flexSelectionListBox,现在可以配合Ctrl进行多行选择循环取值用vsflexgrid.SelectedRows 可以得到你选择的行的总数量然后用循环可以得到具体的行中具体列的内容Dim Temp  As StringDim i As IntegerFor i =
    02-09
点击排行